python操作Mysql数据库

代码:

import MySQLdb

conn=MySQLdb.connect(host='127.0.0.1', user='root', passwd='root', db='test', port=3306)
cur=conn.cursor()
row = cur.execute('select * from stu')
# print affectRow
result = cur.fetchmany(row)
for line in result:
    print line

cur.close()
conn.close()

简单讲解其中用到的函数

MySQLdb.connect() 方法是连接数据库

conn.cursor() 方法是获取游标

cur.execute() 方法是执行sql语句,返回结果集行数

cur.fetchmany() 方法是从结果集中获取结果,参数执行获取的行数,返回值为获取结果

 

posted @ 2014-12-08 11:34  冷冰若水  阅读(139)  评论(0编辑  收藏  举报