天天看點

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() 方法是從結果集中擷取結果,參數執行擷取的行數,傳回值為擷取結果