MySQLdb庫python
import MySQLdbmysql
簡介sql
提供mysql的基本操做(包括建表,讀取表數據,插入數據到表)數據庫
數據庫操做基本步驟app
#!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb if __name__ == "__main__": # 打開數據庫鏈接 db = MySQLdb.connect("localhost", "root", "root", "iquota", charset='utf8') # 使用cursor()方法獲取操做遊標 cursor = db.cursor() # 查看錶 sql_cmd="select * from app;" cursor.execute(sql_cmd) data = cursor.fetchone() data_all = cursor.fetchall() # 關閉數據庫鏈接 db.close() # 讀取一行 print data # 讀取所有內容 print data_all
輸出結果ide
(10L, u'5c26736821d24de48942d982b1c69000', u'qa-stress', u'qa app for stress test', u'zhangsan') ((11L, u'5c26736821d24de48942d982b1c69000', u'qa-stress', u'qa app for stress test', u'lisi'), (12L, u'37ea7ce186404c2ea3a1fb4079d58c15', u'qa-stress-test', u'qa app for stress test', u'zhangsan'), (13L, u'37ea7ce186404c2ea3a1fb4079d58c15', u'qa-stress-test', u'qa app for stress test', u'lisi'), (14L, u'283182bf5ffe4398932d72e9a6d239cc', u'app1', u'add_request', u'lisi'), (21L, u'394c56cde2cb4434a01cff43defcc0a6', u'liurong07Test', u'add_request', u'lisi'))