在上一篇文章里面主要介绍了关于python3连接数据库,创建数据库以及创建表的相关内容,在接下来我们试着在我们刚才创建的表中插入数据,并对其做相关探究。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #/usr/bin/env python #_*_coding:utf-8_*_ #导入pymysql模块 import pymysql #打开数据库链接 connect = pymysql.connect(host = "192.168.186.157" ,port = 3306 ,user = "winner" ,passwd = "123123" ,db = "pymysql" ,charset = "utf8" ,connect_timeout = 3000 ) #使用cursor方法获取操作游标 cursor = connect.cursor() sql = ''' insert into class (name,address) values("JSP","go"),("winner","back"),("GOOD","contine"),("cursor","execute"); ''' #使用execute方法操作数据库 cursor.execute(sql) #事务提交 #connect.commit() data = cursor.execute( "select * from class order by id desc" ) #使用fetchall方法获取操作结果 data = cursor.fetchmany( 5 ) print (data) 注意:在这里将事务提交的部分注释掉了,特演示一下不提交事务的情况。 |
执行结果(执行第四次时):
1 2 3 4 | C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe C: / Users / Administrator / PycharmProjects / python / insertmysql.py (( 12 , 'cursor' , 'execute' ), ( 11 , 'GOOD' , 'contine' ), ( 10 , 'winner' , 'back' ), ( 9 , 'JSP' , 'go' )) Process finished with exit code 0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 检查数据库中的数据: mysql> select database (); + ------------+ | database () | + ------------+ | pymysql | + ------------+ 1 row in set (0.00 sec) mysql> show tables; + -------------------+ | Tables_in_pymysql | + -------------------+ | class | + -------------------+ 1 row in set (0.00 sec) mysql> select * from class; Empty set (0.00 sec) mysql> |
1 2 3 | 经过检查数据库相关表,我们发现此时数据为空,这是为什么呢,回忆一下我们将注释事务提交行 connect . commit () 这里就涉及到mysql数据库有关事务的相关知识, 我们试试加上事务会是什么结果呢?? |
执行结果(手动干预过的显示结果):
1 2 3 4 5 6 7 8 9 | C:\Users\Administrator\AppData\ Local \Programs\Python\Python35\python.exe C:/Users/Administrator/PycharmProjects/python/insertmysql.py ((28, 'cursor' , 'execute' ), (27, 'GOOD' , 'contine' ), (26, 'winner' , 'back' ), (25, 'JSP' , 'go' ), (24, 'cursor' , 'execute' ), (23, 'GOOD' , 'contine' ), (22, 'winner' , 'back' ), (21, 'JSP' , 'go' ), (20, 'cursor' , 'execute' ), (19, 'GOOD' , 'contine' ), (18, 'winner' , 'back' ), (17, 'JSP' , 'go' ), (16, 'cursor' , 'execute' ), (15, 'GOOD' , 'contine' ), (14, 'winner' , 'back' ), (13, 'JSP' , 'go' )) Process finished with exit code 0 |
数据库的查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | mysql> select * from class; + ----+--------+---------+ | id | name | address | + ----+--------+---------+ | 13 | JSP | go | | 14 | winner | back | | 15 | GOOD | contine | | 16 | cursor | execute | | 17 | JSP | go | | 18 | winner | back | | 19 | GOOD | contine | | 20 | cursor | execute | | 21 | JSP | go | | 22 | winner | back | | 23 | GOOD | contine | | 24 | cursor | execute | | 25 | JSP | go | | 26 | winner | back | | 27 | GOOD | contine | | 28 | cursor | execute | + ----+--------+---------+ 16 rows in set (0.00 sec) mysql> |
由此我们发现数据库的事务关系在软件开发的过程当中是相当重要的一部分,所以在对事务处理的时候需要严谨。
提交事务的源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #/usr/bin/env python #_*_coding:utf-8_*_ #导入pymysql模块 import pymysql #打开数据库链接 connect = pymysql.connect(host = "192.168.186.157" ,port = 3306 ,user = "winner" ,passwd = "123123" ,db = "pymysql" ,charset = "utf8" ,connect_timeout = 3000 ) #使用cursor方法获取操作游标 cursor = connect.cursor() sql = ''' insert into class (name,address) values("JSP","go"),("winner","back"),("GOOD","contine"),("cursor","execute"); ''' #使用execute方法操作数据库 cursor.execute(sql) #事务提交 connect.commit() data = cursor.execute( "select * from class order by id desc" ) #使用fetchall方法获取操作结果 data = cursor.fetchall() print (data) |