1.安装pymysql:pip install pymysql (在命令行窗口中执行)
2.卸载pymysql:pip uninstall pymysql (在命令行窗口中执行)
数据库的连接
需要注意的是port是不用引号括起来 charset是utf8不是utf-8
1
2
3
4
5
6
7
8
9
10
11
12
|
# 获取数据库连接对象 connection = pymysql.connect(host = 'localhost' , port = 3306 , user = 'root' , passwd = '2732195202' , db = 'book' , charset = 'utf8' ) # 获取一个游标 driver = connection.cursor() # 执行一条sql driver.execute( "select version()" ) # 获取执行sql的返回值 resultData = driver.fetchall() print (resultData) # 关闭数据库 connection.close() |
创建数据库表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import pymysql #获取数据库连接对象 connection = pymysql.connect(host = 'localhost' , port = 3306 , user = 'root' , passwd = '2732195202' , db = 'book' , charset = 'utf8' ) #获取一个游标 driver = connection.cursor() # 如果该数据库存在就删除 driver.execute( "drop table if exists t_emp " ) # 定义sql语句 sql = """ CREATE TABLE `t_emp` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `department` varchar(20) DEFAULT NULL COMMENT '部门', `salary` decimal(10,2) DEFAULT NULL COMMENT '工资', `age` int(11) DEFAULT NULL COMMENT '年龄', `sex` varchar(4) DEFAULT NULL COMMENT '性别', PRIMARY KEY (`id`) )ENGINE=InnoDB DEFAULT CHARSET=utf8; """ # 执行sql driver.execute(sql) # 关闭数据连接 connection.close() |
向数据库中添加数据
1.需要注意的是规范sql,该写的字段都写上,不使用默认对应
2.提交事务的对象是数据库连接对象,而不是游标对象
3.pycharm连接mysql数据时,如果连接驱动是高版本,需要加上时区,jdbc:mysql://localhost/book?serverTimezone=GMT%2B8
4.如果主键是自动递增,则不能手动指定值,不能写该字段,让其自增长
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# 获取数据库连接对象 connection = pymysql.connect(host = 'localhost' ,port = 3306 ,user = 'root' ,passwd = '2732195202' ,db = 'book' ,charset = 'utf8' ) # 获取一个游标 driver = connection.cursor() # 定义sql语句 sql = """ insert into t_emp(name,department,salary,age,sex) values("tom","开发部",8000,25,"男"), ("tom","开发部",8000,25,"男") """ # 尝试捕捉错误 try : # 执行SQL,并返回收影响行数 result = driver.execute(sql) # 提交事务 connection.commit() print ( "sql(insert)->error" ) except : # 如果发生错误 则回滚事务 print ( "sql(insert)->error" ) driver.rollback() # 关闭数据库连接 connection.close() |
修改表中的数据
注意点:在操作数据库之前,需要确认是否获取连接数据库成功,并且选中了数库
2.卸载第三方库:pip uninstall pymysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#获取数据库连接对象 autocommit=True:设置数据库自动提交 connection = pymysql.connect(host = "localhost" ,port = 3306 ,user = 'root' ,passwd = '2732195202' ,db = 'book' ,charset = 'utf8' ,autocommit = True ) # 获取游标对象 driver = connection.cursor() # 定义sql sql = "update t_emp set salary=%s,name=%s where id=%s;" # 如果sql错误就执行回滚操作,成功就提交 try : # 执行sql,并且返回影响的行数 result = driver.execute(sql,[ 6000 , "admin" , 19 ]) connection.commit() print ( "sql(update)->success" ) except : print ( "sql(update)->error" ) connection.rollback() # 关闭数据库连接对象 connection.close() |
查询数据
1.项目中的.py文件不能和python库中的文件进行冲突,否则会出现异常
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# 获取数据库连接对象 connection = pymysql.connect(host = 'localhost' ,port = 3306 ,user = 'root' ,passwd = '2732195202' ,db = 'book' ,charset = 'utf8' ) # 获取一个游标对象 driver = connection.cursor() #定义sql sql = "select id, name, department, salary, age, sex from t_emp where id>%s and sex=%s" # 只能获取一次,获取多次的时候会获取到null 如果是多个参数,需要传递一个元组 try : driver.execute(sql,( 1 , "女" )) # 获取所有的查询结果 返回一个元组 resultAll = driver.fetchall() print ( "resultAll:" , resultAll) # 获取2条数据 resultTwo = driver.fetchmany( 2 ) print ( "resultTwo:" , resultTwo) # 获取一条数据 resultOne = driver.fetchone() print ( "resultThree:" , resultOne) print ( "sql(select)->success" ) except : connection.rollback() print ( "sql(select)->error" ) # 关闭数据库连接 connection.close() |
删除表中的记录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import pymysql # 获取数据库连接对象 connection = pymysql.connect(host = 'localhost' , port = 3306 , user = 'root' , passwd = '2732195202' , db = 'book' , charset = 'utf8' ) # 获取一个游标 driver = connection.cursor() # 定义sql sql = "delete from t_emp where id=%s" try : # 执行一条sql driver.execute(sql, ( 21 )) # 提交事务 connection.commit() print ( "sql(delete)->success" ) except Exception as e: # 回滚事务 connection.rollback() print ( "sql(delete)->error" ) print (e) #关闭数据库连接 connection.close() |
事务操作
提交事务: connection.commit()
回滚事务: connection.rollback()
总结
到此这篇关于pymsql实现增删改查(python)的文章就介绍到这了,更多相关pymsql增删改查内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/m0_46188681/article/details/116721048