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 ...
随机推荐
- shell 读取yaml 之 shyaml
安装shyaml pip3. install shyaml file.yaml文件内容---idc_group: name: bx bx: news_bx: news_bx web3_bx: web3 ...
- 企业级Nginx负载均衡与keepalived高可用实战(一)Nginx篇
1.集群简介 1.1.什么是集群 简单地说,集群就是指一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行各自服务的独立服务器. ...
- CentOS7部署vsftpd服务
1.查看是否已经安装了vsftpd vsftpd -version 2.安装vsftpd(CentOS7) yum install -y vsftpd 3.新建FTP目录 创建的FTP目录如下: /d ...
- [转帖]从零开始入门 K8s | 手把手带你理解 etcd
从零开始入门 K8s | 手把手带你理解 etcd https://zhuanlan.zhihu.com/p/96721097 导读:etcd 是用于共享配置和服务发现的分布式.一致性的 KV 存储系 ...
- oracle查看执行计划入门
基于Oracle的应用系统很多的性能问题都是由应用系统的SQL性能低劣引起的,因此SQL的性能优化非常重要.要分析与优化SQL的性能,一般是通过查看该SQL的执行计划,然后通过执行计划有针对性地对SQ ...
- UWP 使用Launcher 启动迅雷
不得不说UWP有些地方真的不方便! 另外也要夸一下迅雷,还是蛮不错的! 代码 await Launcher.LaunchUriAsync(new Uri("magnet:?xt") ...
- WPF程序集资源
WPF会将引用到的资源如图片.BAML文件等编译成二进制数据嵌入到已经编译了的程序集中. 下图是一个反编译后的程序目录结构: 那么,如何向项目中添加资源? 向项目中添加文件 设置生成操作(Build ...
- C# 身份证号码15位和18位验证
/// <summary> /// 身份证 /// </summary> [Serializable] public class IDCard { /// <su ...
- 我为什么学习Haskell
说起来,Haskell真是相当冷门而小众的一门语言.在我工作第一年的时候,我平时从网络的一些学习资料上时不时看到有人提到这门语言.那时候的认识就是除了我们平时用的“面向对象语言 (OOP: Objec ...
- zookeeper知识点总结
1. 关于ZooKeeper集群服务器数: ZooKeeper 官方确实给出了关于奇数的建议,但绝大部分 ZooKeeper 用户对于这个建议认识有偏差.一个 ZooKeeper 集群如果要对外提供可 ...