在上一篇文章里面主要介绍了关于python3连接数据库,创建数据库以及创建表的相关内容,在接下来我们试着在我们刚才创建的表中插入数据,并对其做相关探究。
#/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)
注意:在这里将事务提交的部分注释掉了,特演示一下不提交事务的情况。
执行结果(执行第四次时):
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
检查数据库中的数据:
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>
经过检查数据库相关表,我们发现此时数据为空,这是为什么呢,回忆一下我们将注释事务提交行
connect.commit() 这里就涉及到mysql数据库有关事务的相关知识,
我们试试加上事务会是什么结果呢??
执行结果(手动干预过的显示结果):
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
数据库的查询结果:
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>
由此我们发现数据库的事务关系在软件开发的过程当中是相当重要的一部分,所以在对事务处理的时候需要严谨。
提交事务的源代码:
#/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)
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。