???????
?????????????????С????????????????檔?????н???????????????????С?
???????Python?????????????????????
?????????????server???氳???mysql????????????????????????????????????????????????????????£???????
????python??mysql?????
????????????????MySQLdb????????????????????????python?????????mysql?????????mysql??c????API??MySQLdb??????????????????????????????????????????????mysql?????????????????MySQLdb
???????????????????
????????????http://sourceforge.net/projects/mysql-python/?????????°汾???MySQLdb??????????а?????????????Щ????
???????python setup.py build ???а???????No module named setuptools
?????????????????
????sudo apt-get install python-setuptools
??????????У???????????? mysql_config not found
?????????????????mysqld-dev
????sudo apt-get install libmysqld-dev
??????????????л????????????????? `
????building ‘mysql’ extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1??2??3??’final’??0) -Dversion=1.2.3 -I/usr/include/mysql -I/usr/include/python2.7 -c mysql.c -o build/temp.linux-i686-2.7/mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX In file included from mysql.c:29:0: pymemcompat.h:10:20: fatal error: Python.h: No such file or directory
??????????
????sudo apt-get install python-dev
????????????python???Щ?????????????
????????????????????????????????????????
???????????mysql???????????????lib?????з??/usr/local/lib???????????
?????????????????????????????????£???????????/etc/ld.so.cnf???????????lib?????????з????????????????????????ldconfig????????Ч?????
???????????????????? ln -s /usr/local/mysql/lib/mysql/libmysqlclient* /usr/lib
??????????
????????MySQLdb??
????import MySQLdb
?????????????
????conn=MySQLdb.connect(host=“localhost”??user=“root”??passwd=“sa”??db=“mytable”??charset=“utf8”)
????????connect??????????????????????????????????????????????.
????????????????
????cursor=conn.cursor() n=cursor.execute(sql??param)
??????????????????????????????cursor????????????????????cursor????????????й???.??Щ??????????????:1.????????2.????????
???????????????????????
??????????????????????
???????????????????????????.??????????????????
????cursor.close()
????conn.close()
??????????ò???API
??????????????????????????? commit() ??
????rollback() ???
????cursor????????????????:
????callproc(self?? procname?? args):???????д洢???????????????洢????????????б??????????????????
????execute(self?? query?? args):???е???sql????????????sql??????????????б??????????????????
????executemany(self?? query?? args):???е???sql??????????????????б????????????????????????? nextset(self):???????????????
????cursor?????????????????:
????fetchall(self):???????е????????.
????fetchmany(self?? size=None):????size??????????.????size??????????????е????????????cursor.arraysize??????.
????fetchone(self):????????????.
????scroll(self?? value?? mode=‘relative’):??????????.????mode=‘relative’???????????????????value???????? mode=‘absolute’?????????????????????value??.
??????????
??????????????ubuntu14.04??????????????????????????δ?github?????????????м????????????????????????2??????????????????????????????