用python操做数据库,特别是作性能测试造存量数据时特别简单方便,比存储过程方便多了。html
前提:安装mysql、python,参考:http://www.javashuo.com/article/p-dnexrcey-bg.htmlpython
数据库qzcsjb的test表中初始化的数据:mysql
安装pymysql模块,pip install pymysqlsql
import pymysql # 创建数据库链接 conn=pymysql.connect( host='192.168.168.168', port=3306, user='root', password='mysql', db='qzcsbj', charset='utf8' ) # 获取游标 cursor=conn.cursor() # 执行sql语句 sql = 'select * from test where name = "%s" and id="%s"' %('qzcsbj1','1') rows=cursor.execute(sql) # 返回结果是受影响的行数 # 关闭游标 cursor.close() # 关闭链接 conn.close() # 判断是否链接成功 if rows >= 0: print('链接数据库成功') else: print('链接数据库失败')
import pymysql # 创建数据库链接 conn=pymysql.connect( host='192.168.168.168', port=3306, user='root', password='mysql', db='qzcsbj', charset='utf8' ) # 获取游标 cursor=conn.cursor() # 执行sql语句 sql='insert into test(id,name) values(%s,%s)' rows=cursor.execute(sql,('4','qzcsbj4')) # 提交 conn.commit() # 关闭游标 cursor.close() # 关闭链接 conn.close()
import pymysql # 创建数据库链接 conn=pymysql.connect( host='192.168.168.168', port=3306, user='root', password='mysql', db='qzcsbj', charset='utf8' ) # 获取游标 cursor=conn.cursor() # 执行sql语句 sql='insert into test(id,name) values(%s,%s)' rows=cursor.executemany(sql,[('5','qzcsbj5'),('6','qzcsbj6'),('7','qzcsbj7')]) # 提交 conn.commit() # 关闭游标 cursor.close() # 关闭链接 conn.close()
import pymysql # 创建数据库链接 conn=pymysql.connect( host='192.168.168.168', port=3306, user='root', password='mysql', db='qzcsbj', charset='utf8' ) # 获取游标 cursor=conn.cursor(pymysql.cursors.DictCursor) # 执行sql语句 values=[] for i in range(100, 201): values.append((i, 'qzcsbj'+str(i))) sql='insert into test(id,name) values(%s,%s)' rows=cursor.executemany(sql,values) # 提交 conn.commit() # 关闭游标 cursor.close() # 关闭链接 conn.close()
把上面大批量新增的数据删除,delete from test where id>=100;数据库
import pymysql # 创建数据库链接 conn=pymysql.connect( host='192.168.168.168', port=3306, user='root', password='mysql', db='qzcsbj', charset='utf8' ) # 获取游标 cursor=conn.cursor() # 执行sql语句 sql='update test set name = %s where id = %s' rows=cursor.execute(sql,('qzcsbj','7')) # 提交 conn.commit() # 关闭游标 cursor.close() # 关闭链接 conn.close()
import pymysql # 创建数据库链接 conn=pymysql.connect( host='192.168.168.168', port=3306, user='root', password='mysql', db='qzcsbj', charset='utf8' ) # 获取游标 cursor=conn.cursor() # 执行sql语句 sql='update test set name = %s where id = %s' rows=cursor.executemany(sql,[('全栈测试笔记5','5'),('全栈测试笔记6','6')]) # 提交 conn.commit() # 关闭游标 cursor.close() # 关闭链接 conn.close()
下面脚本和上面增长数据,除了执行sql语句部分不同,其他都同样app
# 执行sql语句 sql='delete from test where id = %s' rows=cursor.execute(sql,('1',))
下面脚本和上面增长数据,除了执行sql语句部分不同,其他都同样性能
# 执行sql语句 sql='delete from test where id = %s' rows=cursor.executemany(sql,[('2'),('3')])
有点像从管道中取一个,若是再来一个fetchone,会又取下一个,若是取完了再取,就返回None测试
每条记录为元组格式fetch
下面脚本和上面增长数据,除了执行sql语句部分不同,其他都同样3d
# 执行sql语句 rows=cursor.execute('select * from test;') print(cursor.fetchone()) print(cursor.fetchone()) print(cursor.fetchone()) print(cursor.fetchone()) print(cursor.fetchone())
运行结果:
(4, 'qzcsbj4')
(5, '全栈测试笔记5')
(6, '全栈测试笔记6')
(7, 'qzcsbj')
None
每条记录为字典格式
# 获取游标 cursor=conn.cursor(pymysql.cursors.DictCursor) # 执行sql语句 rows=cursor.execute('select * from test;') print(cursor.fetchone()) print(cursor.fetchone()) print(cursor.fetchone()) print(cursor.fetchone()) print(cursor.fetchone())
运行结果:
{'id': 4, 'name': 'qzcsbj4'}
{'id': 5, 'name': '全栈测试笔记5'}
{'id': 6, 'name': '全栈测试笔记6'}
{'id': 7, 'name': 'qzcsbj'}
None
# 获取游标 cursor=conn.cursor(pymysql.cursors.DictCursor) # 执行sql语句 rows=cursor.execute('select * from test;') print(cursor.fetchmany(2))
运行结果:
[{'id': 4, 'name': 'qzcsbj4'}, {'id': 5, 'name': '全栈测试笔记5'}]
# 获取游标 cursor=conn.cursor(pymysql.cursors.DictCursor) # 执行sql语句 rows=cursor.execute('select * from test;') print(cursor.fetchall()) print(cursor.fetchall())
运行结果:
[{'id': 4, 'name': 'qzcsbj4'}, {'id': 5, 'name': '全栈测试笔记5'}, {'id': 6, 'name': '全栈测试笔记6'}, {'id': 7, 'name': 'qzcsbj'}]
[]
从头开始跳过n个
# 获取游标 cursor=conn.cursor(pymysql.cursors.DictCursor) # 执行sql语句 rows=cursor.execute('select * from test;') cursor.scroll(3,mode='absolute') print(cursor.fetchone())
运行结果:
{'id': 7, 'name': 'qzcsbj'}
# 获取游标 cursor=conn.cursor(pymysql.cursors.DictCursor) # 执行sql语句 rows=cursor.execute('select * from test;') print(cursor.fetchone()) cursor.scroll(2,mode='relative') print(cursor.fetchone())
运行结果:
{'id': 4, 'name': 'qzcsbj4'}{'id': 7, 'name': 'qzcsbj'}