pythonリンクMySQLデータベース

1808 ワード

PythonのDB-API使用フロー:1、APIモジュールの導入2、データベースへのリンクの取得3、SQL文とストアド・プロシージャの実行4、データベースのシャットダウン
データベース・クエリー・アクション
# encoding:utf-8
import MySQLdb
db = MySQLdb.connect("localhost","   ","  ","    ")
cursor = sb.cursor()
#   execute    SQL  
sql = "select * from   where id > '%d" % (1000)
try:
    cursor.execute(sql)
    #         
    results = cursor.fetchall()
    for row in results :
        firstname = row[0]
        lastname = row[1]
        age = row[2]
        sex = row[3]
        income = row[4]
        city = row[4]
        print "firstname=%s,lastname=%s,age=%d,sex=%s,income=%d,city=%d" % (firstname,lastname,age,sex,income,city)
except:
    print "error: unable to fecth data"
# close db
db.close()