用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(增、删、改、查)的更多相关文章

  1. php5.4以上 mysqli 实例操作mysql 增,删,改,查

    <?php //php5.4以上 mysqli 实例操作mysql header("Content-type:text/html;charset=utf8"); $conn ...

  2. Go语言之进阶篇mysql增 删 改 查

    一.mysql操作基本语法 1.创建名称nulige的数据库 CREATE DATABASE nulige DEFAULT CHARSET utf8 COLLATE utf8_general_ci; ...

  3. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  4. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  5. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  6. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  7. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  8. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  9. 洗礼灵魂,修炼python(91)-- 知识拾遗篇 —— pymysql模块之python操作mysql增删改查

    首先你得学会基本的mysql操作语句:mysql学习 其次,python要想操作mysql,靠python的内置模块是不行的,而如果通过os模块调用cmd命令虽然原理上是可以的,但是还是不太方便,那么 ...

  10. python基础中的四大天王-增-删-改-查

    列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...

随机推荐

  1. [LeetCode] 465. Optimal Account Balancing 最优账户平衡

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  2. HTML+css基础 三大列表

    三大列表: 1.无序列表  ul  标签属性type  决定项目符号的类型  disc(实心圆)square (方形) circle 空心圆     里面的子标签是li   2.有序列表 ol   t ...

  3. unity资源机制(转)

    原文地址:https://www.jianshu.com/p/ca5cb9d910c0作者:重装机霸 2.资源概述 Unity必须通过导入将所支持的资源序列化,生成AssetComponents后,才 ...

  4. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  5. MSSQL 生成唯一自增数据的办法

    我的应用场景是多进程并发获取这个计数,且要保证唯一且自增,我用的办法是锁表 计数表Counter,就一行数据 下面是存储过程 create procedure [dbo].[GetCount] AS ...

  6. 新一代ActiveMQ —— Apache ActiveMQ Artemis

    资料: .net demo : https://github.com/apache/activemq-artemis/tree/master/examples/protocols/amqp/dotne ...

  7. C# winform窗体简单保存界面控件参数到xml

    引用网上的 XMLHelper 地址 :https://www.cnblogs.com/chnboy/archive/2009/04/02/1427652.html 稍做修改 using System ...

  8. tensorflow常用函数(二)

    一.变量相关的函数 1)tf.train.list_variables(ckpt_dir_or_file)    Returns list of all variables in the checkp ...

  9. 活动任务出现bug

    之前做的一个活动任务发现一个bug,是以前和离职同事一起对逻辑的时候没有考虑到的,配置活动的时候应该要先查询下,如果这个产品线上在这段时间已经配置了并且上线了,则不能在做活动处理了,否则就和前面的活动 ...

  10. Vue.js最佳实践--给大量子孙组件传值(provide/inject)

    开发中有个需求,有个Parent组件(例如div)下,输入框,下拉框,radiobutton等可编辑的子孙组件几百个,根据某个值统一控制Parent下面的所有控件的disabled状态 类似于这样,给 ...