python連結mysql查詢資料例項

pythontab發表於2013-05-06

使用 MySQLdb 模組連線 MySQL

# coding=utf-8
 
#引入mysql python客戶端模組
import MySQLdb
import sys
#進行資料庫連線
conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="pythontab", charset="utf8")
cursor = conn.cursor()
#執行sql
cursor.execute("select * from test")
#列出資料
records = cursor.fetchall()
for row in records:
    for r in row:
        print r


相關文章