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 ...
随机推荐
- CISCO 3750交换机堆叠
双交换机堆叠操作 一.基本要求: ios版本要一致.专用的堆叠模块和堆叠线缆.最大堆叠个数9 二.堆叠的好处: 高密度端口.便于管理.堆叠的交换机可以看作一台交换机统一配置 三.堆叠实例: 1:分别清 ...
- java web开发入门八(ssm整合)基于intellig idea
ssm整合 一.导入相关包 二.开发流程 1.写entity package com.eggtwo.euq.entity; import java.io.Serializable; import ja ...
- vue组件component没效果
如果实在不知道问题所在,你就看看你的component的命名是不是驼峰命名
- 父组件调用子组件 viewChild
父组件调用子组件 1.在子组件的ts中声明一个变量 public lineout:any="你好,我是被父组件调用的子组件"; 2.在父组件的html中写入 (引入子组件) & ...
- vue表单验证不通过,依然能执行点击事件里面的代码?
遇到的问题:表单提交的时候,写了rules,明明验证不通过依然执行了点击事件里面的代码. 这个验证有什么用? 后来 我看elementUI组件才发现,我漏写了几行代码. methods里面这样写 完美 ...
- js对数组去重的方法总结-(2019-1)
最近待业在家,系统地学习了一套js的课程.虽然工作时间真的比较长了,但有些东西只局限在知其然而不知其所以然的程度上,有些知识点通过“血和泪”的经验积累下来,也只是记了结果并没有深究,所以每次听完课都有 ...
- Docker 部署ELK
1.安装docker前安装pip sudo yum -y install epel-release sudo yum install python-pip 2.安装docker #安装依赖包 yum ...
- Android Studio中的非项目文件及项目目录下的全局搜索
一.背景 项目开发中,AS(Android Studio)经常会用到通过关键字在项目空间下搜索对应结果.最经常用到的Find in Path.例如打开Find in Path后,可以选中Scope t ...
- Spring-Boot-操作-Redis,三种方案全解析!
在 Redis 出现之前,我们的缓存框架各种各样,有了 Redis ,缓存方案基本上都统一了,关于 Redis,松哥之前有一个系列教程,尚不了解 Redis 的小伙伴可以参考这个教程: Redis 教 ...
- 【spring boot】加载同名Bean解决方法
原文地址:https://blog.csdn.net/liuyueyi25/article/details/83280239 @SpringBootApplication @ComponentScan ...