您的位置:首页 > 编程语言 > Python开发

python deal with mysql

2016-03-09 10:35 721 查看

0. install

yum -y install python-dev
pip install MySQL-python


这个报名容易记错。是MySQL-python,注意大小写。

1.连接数据库

import MySQLdb
db = MySQLdb.connect
("localhost","username","passwd","DBNAME",charset='utf8')


2.执行sql语句

“`

sqs = “select host from processlist”

cursor = db.cursor()

cursor.execute(sql)

results = cursor.fetchall()

for record in results:

col1 = record[0]

col2 = record[1]

3.close

db.close()

4.doc

文档很短。这是他蛋疼的地方,毕竟外部库。

http://mysql-python.sourceforge.net/MySQLdb.html

最后总结一下workflow

db = MySQLdb.connect()

cursor = db.cursor()

sql = “”

cursor.execute()

results = cursor.fetchall()

do with results

db.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: