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'))