首页 > 代码库 > python操作mysql数据库(二)
python操作mysql数据库(二)
在上一篇文章里面主要介绍了关于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=http://www.mamicode.com/cursor.execute("select * from class order by id desc" ) #使用fetchall方法获取操作结果 data=http://www.mamicode.com/cursor.fetchmany(5)>执行结果(执行第四次时):
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=http://www.mamicode.com/cursor.execute("select * from class order by id desc" ) #使用fetchall方法获取操作结果 data=http://www.mamicode.com/cursor.fetchall()>本文出自 “坚持梦想” 博客,请务必保留此出处http://dreamlinux.blog.51cto.com/9079323/1907607
python操作mysql数据库(二)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。