Python中pymysql模块通过获取mysql数据库命令行游标执行数据库命令来进行数据库操作

  优点:操作数据库语句所见即所得,执行了什么数据库语句都很清楚

  缺点:操作繁琐,代码量多

1. pymysql的基本使用

# -*- coding:utf-8 -*-
# Author:Wong Du import pymysql # 创建链接,相当于建立一个socket
conn = pymysql.Connection(host='10.0.0.100', port=3306, user='root', passwd='123456', db='testdb') # 建立游标,相当于进入 mysql> 命令操作界面
cursor = conn.cursor() # 建表,和mysql命令行操作一样
try:
create_table = cursor.execute('''create table student(
id int not null primary key auto_increment,
name char(32) not null,
register_date date not null DEFAULT "2018-05-09" );
''')
except pymysql.err.InternalError as e:
# print(type(e))
print("\033[31;1m%s; Do nothing...\033[0m" %e) # 插入数据
insert = cursor.execute('insert into student (name,register_date) values("junry", "2017-03-14");')
insert2 = cursor.execute('insert into student (name,register_date) values("hongfa", "2015-03-14");')
insert3 = cursor.execute('insert into student (name,register_date) values("jinglin", "2016-03-14");') # 查看表数据
select = cursor.execute('select * from student;')
for line in cursor.fetchall():
print(line) # 修改表数据
update = cursor.execute('update student set name="junwei" where id=1')
select2 = cursor.execute('select * from student;')
print(cursor.fetchone()) # 删除表数据
delete = cursor.execute('delete from student;')
select3 = cursor.execute('select * from student;')
if cursor.fetchall():
print(cursor.fetchall())
else:
print("This is a empty table...") # 提交
conn.commit() cursor.close() # 关闭游标
conn.close() # 关闭连接 # 等等 等等。。。

循环插入数据

# -*- coding:utf-8 -*-
# Author:Wong Du import pymysql # 建立连接
conn = pymysql.Connect(host='10.0.0.100', port=3306, user='root', passwd='123456', db='testdb') # 创建游标
cursor = conn.cursor() # 循环插入列表
many_list = [
('zhangsan', '2011-11-11'),
('lisi', '2012-11-11'),
('wangwu', '2022-10-09'),
] # 循环插入(插入多条内容)
cursor.executemany("insert into student (name, register_date) VALUE(%s, %s);", many_list) # 修改游标位置
cursor.scroll(1, mode='relative') # 相对移动,默认为relative
cursor.scroll(1, mode='absolute') # 绝对移动 # fetchone()获取一行数据、fetchmany(num)获取指定行数据、fetchall()获取所有行数据
cursor.execute("select * from student;")
for line in cursor.fetchall():
print(line) # 清楚student表的数据
cursor.execute("delete from student;") # 提交
conn.commit() cursor.close() # 关闭游标
conn.close() # 关闭连接

Python中pymysql基本使用的更多相关文章

  1. python中pymysql使用

    python中pymysql使用 https://blog.csdn.net/johline/article/details/69549131 import pymysql # 连接数据库 conne ...

  2. Python中pymysql模块详解

    安装 pip install pymysql 使用操作 执行SQL #!/usr/bin/env pytho # -*- coding:utf-8 -*- import pymysql # 创建连接 ...

  3. python中pymysql executemany 批量插入数据

    import pymysqlimport timedb = pymysql.connect("IP","username","password&quo ...

  4. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

  5. (转)Python中操作mysql的pymysql模块详解

    原文:https://www.cnblogs.com/wt11/p/6141225.html https://shockerli.net/post/python3-pymysql/----Python ...

  6. python中pymsql常用方法(1)

    python中pymysql模块常用方法以及其使用 首先我们知道pymysql 是python中操作数据库的模块 使用步骤分为如下几步: ​ 1.与数据库服务器建立链接 conn=pymysql.Co ...

  7. python之pymysql模块学习(待完善...)

    pymysql介绍 pymysql是在python3.x版本中用于连接mysql服务器的一个库.python2中则使用mysqldb. 安装方法: pip install pymysql 使用实例: ...

  8. Python中的SQLAlchemy

    在Python中,使用SQLAlchemy可以对数据库进行操作. SQLAlchemy是Python中的一个标准库. 要使用SQLAlchemy,首先要创建连接: url = mysql+pymysq ...

  9. python的pymysql使用方法【转】

    前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.6.6.m ...

随机推荐

  1. 【二食堂】Alpha - 测试报告

    TextMarking Alpha阶段测试报告 前后端测试过程及结果 在Alpha阶段,测试工作紧跟后端开发进度,一下是我们所做的一些测试工作. 后端单元测试 测试代码可以在git仓库中查看,后端对所 ...

  2. [技术博客]WEB实现划词右键操作

    [技术博客]WEB实现划词右键操作 一.功能解释 简单地对题目中描述的功能进行解释:在浏览器中,通过拖动鼠标选中一个词(或一段文字),右键弹出菜单,且菜单为自定义菜单,而非浏览器本身的菜单.类似的功能 ...

  3. vs2010中使用命令行参数

    使用VS2010增加命令参数的时候老是不起作用,后面经过研究发现,所要增加的命令参数是一个相对文件路径,而默认的工作目录里面没有该文件,所以就没有找到,需要修改工作目录,这样命令行参数才能够起作用.

  4. MIPI的走线阻抗

    MIPI的走线阻抗100欧的要求是根据LVDS(Low Voltage Differential Signaling)电平定义的. LVDS差分信号PN两线最大幅度是350mV,内部一个恒流源电流是3 ...

  5. MVC中单选按钮的实现

    -------------控制器-------------- ViewBag.Kinds = SYS_Category.List(xxxxxxxxxxxxxxxxxxxxxxx); --------- ...

  6. Mac搭建以太坊私有链

    记录过程与问题 一.安装 以go版本的ethereum进行安装 brew tap ethereum/ethereum brew install ethereum # 如果希望基于ethereum的de ...

  7. Allure 生成测试报表

    Allure官方文档参考地址:https://docs.qameta.io/allure/#_testng 1.在maven中添加依赖并进行相应的配置: <!-- 实现版本控制 --> & ...

  8. 用 python 解决线性代数中的矩阵运算

    用 python 解决线性代数中的矩阵运算 矩阵叉乘 矩阵求逆 矩阵转置 假定AX=B,求解未知矩阵X 矩阵的行列式值|matrix| 未完待续..... import sys from PyQt5. ...

  9. Vue.js教程 1.前端框架学习介绍

    Vue.js教程 1.前端框架学习介绍 什么是Vue.js 为什么要学习流行框架 什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站, ...

  10. C 语言基础,来喽!

    前言 C 语言是一门抽象的.面向过程的语言,C 语言广泛应用于底层开发,C 语言在计算机体系中占据着不可替代的作用,可以说 C 语言是编程的基础,也就是说,不管你学习任何语言,都应该把 C 语言放在首 ...