MySQLdb库
import MySQLdb
简介
提供mysql的基本操作(包括建表,读取表数据,插入数据到表)
数据库操作基本步骤
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#!/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
输出结果
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
(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'))