全栈工程师开发手册 (作者:陈玓玏)
python教程全解
调试环境python3.6,调试python操作mysql数据库,首先要在本地或服务器安装mysql数据库。安装参考:http://blog.csdn.net/luanpeng825485697/article/details/77816790。
在python2.7下,我们使用MySQLdb库点击下载
在python3.6下我们使用pymysql库点击下载
安装python库的方法,请查看Python库的安装与卸载
安装成功后就可以编程代码实现python对mysql数据库的操作了
python3.6下代码如下
复制代码
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117#coding:utf-8 #python3.6使用pymysql操作mysql print("=====================mysql数据库=====================") import pymysql.cursors # 连接数据库 connect = pymysql.Connect( host='127.0.0.1', port=3306, user='root', passwd='19910101a', db='note', charset='utf8' ) # 获取游标 cursor = connect.cursor() #删除表 sql = 'DROP TABLE IF EXISTS student' cursor.execute(sql) connect.commit() print('如果存在表就删除表格') #创建表格 sql = "CREATE TABLE student(id INTEGER PRIMARY KEY,name TEXT)" try: cursor.execute(sql) connect.commit() except: print("表已存在") print('成功创建表格') # 插入数据 sql = "INSERT INTO student VALUES(%d,'%s')" data = (1, 'student1') cursor.execute(sql % data) connect.commit() print('成功插入', cursor.rowcount, '条数据') # 查询数据方法2,注意这种方式会自动帮你添加引号 sql = "INSERT INTO student VALUES(%s,%s)" data = (1, 'student1') cursor.execute(sql, data) connect.commit() print('成功插入', cursor.rowcount, '条数据') # 修改数据 sql = "UPDATE student SET name = '%s' WHERE id = %d " data = ('student2', 1) cursor.execute(sql % data) connect.commit() print('成功修改', cursor.rowcount, '条数据') # 查询数据 sql = "SELECT * FROM student WHERE id=%d" data = (1,) cursor.execute(sql % data) for row in cursor.fetchall(): print("%s" % str(row)) print('共查找出', cursor.rowcount, '条数据') # 删除数据 sql = "DELETE FROM student WHERE id = %d LIMIT %d" data = (1, 1) cursor.execute(sql % data) connect.commit() print('成功删除', cursor.rowcount, '条数据') # 事务处理 sql_1 = "UPDATE student SET name = name + '1' WHERE id = 1 " try: cursor.execute(sql_1) except Exception as e: connect.rollback() # 事务回滚 print('事务处理失败', e) else: connect.commit() # 事务提交 print('事务处理成功', cursor.rowcount) # 关闭连接 cursor.close() connect.close() #pymysql.Connect()参数说明 #host(str): MySQL服务器地址 #port(int): MySQL服务器端口号 #user(str): 用户名 #passwd(str): 密码 #db(str): 数据库名称 #charset(str): 连接编码 # #connection对象支持的方法 #cursor() 使用该连接创建并返回游标 #commit() 提交当前事务 #rollback() 回滚当前事务 #close() 关闭连接 # #cursor对象支持的方法 #execute(op) 执行一个数据库的查询命令 #fetchone() 取得结果集的下一行 #fetchmany(size) 获取结果集的下几行 #fetchall() 获取结果集中的所有行 #rowcount() 返回数据条数或影响行数 #close() 关闭游标对象
python2.7下代码如下
如果连接失败会报错。
复制代码
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
28
29
30#coding:utf-8 #python2.7使用MySQLdb操作mysql import MySQLdb print("=====================mysql数据库=====================") getRC = lambda cur: cur.rowcount if hasattr(cur, 'rowcount') else -1 #获取游标所指向是数据的行数 HOST = "127.0.0.1" #数据库主机 USER = 'root' #用户名 PASSWORD = '111111' #密码 DBNAME = 'note' #数据名称 try: conn = MySQLdb.connect(HOST,USER,PASSWORD,DBNAME) #连接mysql数据库,参数:主机号、用户名、密码、数据库 curs=conn.cursor() # 获取游标 curs.execute('CREATE TABLE student(id INTEGER PRIMARY KEY,name TEXT)') # 执行代码,创建表和字段 curs.execute("INSERT INTO student VALUES(1,'student1')"); # 添加记录 curs.execute("INSERT INTO student VALUES(%d, '%s')" % (2, 'student2')) #添加记录 conn.commit() # 每次执行完后都应该保存 except Exception:print("数据表和记录已经添加") finally: curs.execute("UPDATE student SET name='student3' WHERE id=2") #更新记录 curs.execute("SELECT * FROM student") # 查询记录 for row in curs.fetchall(): print(row[0],row[1]) curs.execute('DELETE FROM student WHERE id=%d' % 1) #删除记录 curs.execute('DROP TABLE student') #删除表 curs.close() #关闭游标 conn.close() #关闭连接
最后
以上就是等待冰棍最近收集整理的关于python数据存储系列教程——python中mysql数据库操作:连接、增删查改、指令执行的全部内容,更多相关python数据存储系列教程——python中mysql数据库操作内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复