python操作mysql(增、删、改、查)
用python操作数据库,特别是做性能测试造存量数据时特别简单方便,比存储过程方便多了。
连接数据库
前提:安装mysql、python,参考:https://www.cnblogs.com/UncleYong/p/10530261.html
数据库qzcsjb的test表中初始化的数据:

安装pymysql模块,pip install pymysql
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语句部分不一样,其余都一样
# 执行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
有点像从管道中取一个,如果再来一个fetchone,会又取下一个,如果取完了再取,就返回None
每条记录为元组格式
下面脚本和上面增加数据,除了执行sql语句部分不一样,其余都一样
# 执行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
fetchmany
# 获取游标
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'}]
fetchall
# 获取游标
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'}
python操作mysql(增、删、改、查)的更多相关文章
- php5.4以上 mysqli 实例操作mysql 增,删,改,查
<?php //php5.4以上 mysqli 实例操作mysql header("Content-type:text/html;charset=utf8"); $conn ...
- Go语言之进阶篇mysql增 删 改 查
一.mysql操作基本语法 1.创建名称nulige的数据库 CREATE DATABASE nulige DEFAULT CHARSET utf8 COLLATE utf8_general_ci; ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
- 洗礼灵魂,修炼python(91)-- 知识拾遗篇 —— pymysql模块之python操作mysql增删改查
首先你得学会基本的mysql操作语句:mysql学习 其次,python要想操作mysql,靠python的内置模块是不行的,而如果通过os模块调用cmd命令虽然原理上是可以的,但是还是不太方便,那么 ...
- python基础中的四大天王-增-删-改-查
列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...
随机推荐
- [LeetCode] 253. Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 62. Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [转]Visual Studio 2017各版本安装包离线下载、安装全解析
Visual Studio 2017各版本安装包离线下载.安装全解析 2017-3-10 11:15:03来源:IT之家作者:寂靜·櫻花雨责编:晨风评论:165 感谢IT之家网友 寂靜·櫻花雨的投 ...
- 团队作业第五次—项目冲刺-Day2
Day2 part1-SCRUM: 项目相关 作业相关 具体描述 所属班级 2019秋福大软件工程实践Z班 作业要求 团队作业第五次-项目冲刺 作业正文 hunter--冲刺集合 团队名称 hunte ...
- [转载]3.14 UiPath图片操作截图的介绍和使用
一.截图(Take Screenshot)的介绍 截取指定的UI元素屏幕截图的一种活动,输出量仅支持图像变量(image) 二.Take Screenshot在UiPath中的使用 1.打开设计器,在 ...
- dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gold;第一次无效
private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {}//修改DataGrid ...
- 好用的低延迟vps
ZeptoVM是一个俄罗斯的云提供商, 由于提供了黑龙江北边的机房, 所以延迟比较低 注意一定要选Khabarovsk节点, 这个节点延迟很低, 我在上海延迟大约有70ms 缺点就是比较贵, 按照年付 ...
- Maven依赖以及项目创建
目录: 1. Maven依赖.Eclipse中使用Maven.生命周期 1.1 Maven依赖 1.2 Eclipse中使用Maven 2. 依赖排除.通过Maven整合多个Maven 2.1 依赖排 ...
- 31,Leetcode下一个排列 - C++ 原地算法
题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...
- Mysql序列(八)—— group by排序问题
在mysql中,group by默认会执行排序: By default, MySQL sorts GROUP BY col1, col2, ... queries as if you also inc ...