安装依赖:html
sudo apt-get install libmysqlclient-dev libmysqld-dev python-dev python-setuptools
安装MySQLdbpython
pip install MySQL-python
conn = mdb.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='test', charset='utf8')
config = {
'host': '127.0.0.1',
'port': 3306,
'user': 'root',
'passwd': 'root',
'db': 'fzk',
'charset': 'utf8'
}
conn = db.connect(**config)
conn.autocommit(1)mysql
cursor=conn.cursor()
n=cursor.execute(sql,param)
首先,咱们用使用链接对象得到一个cursor对象,接下来,咱们会使用cursor提供的方法来进行工做.这些方法包括两大类:1.执行命令,2.接收返回值
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条.sql
用户指南:http://mysql-python.sourceforge.net/MySQLdb.html
API:http://mysql-python.sourceforge.net/MySQLdb-1.2.2/fetch