本文实例讲述了Python操作mysql数据库实现增删查改功能的方法。分享给大家供大家参考,具体如下:
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
#coding=utf-8
import MySQLdb
class Mysql_Oper:
def __init__( self ,host,user,passwd,db):
self .host = host
self .user = user
self .passwd = passwd
self .database = db
def db_connecet( self ):
try :
#连接
conn = MySQLdb.connect(host = self .host,user = self .user,passwd = self .passwd,db = self .database,charset = "utf8" )
cursor = conn.cursor()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[ 0 ], e.args[ 1 ])
def drop_table( self ,table):
try :
#删除表
sql = "drop table if exists" + table
cursor.execute(sql)
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[ 0 ], e.args[ 1 ])
def create_table( self ,table):
try :
if table = = "dept" :
#创建
sql = "create table if not exists dept(deptno int primary key, dname varchar(50),loc varchar(50))"
cursor.execute(sql)
elif table = = "emp" :
sql = = "create table if not exists emp(empno INT PRIMARY KEY,ename VARCHAR(50),job VARCHAR(50),mgr INT,hiredate DATE,sal DECIMAL(7,2),COMM DECIMAL(7,2),deptno INT,loc varchar(50),CONSTRAINT fk_emp FOREIGN KEY(mgr) REFERENCES emp(empno))"
cursor.execute(sql)
elif table = = "salgrade" :
sql = "create table if not exists salgrade(grade INT PRIMARY KEY,losal INT,hisal INT)"
cursor.execute(sql)
elif table = = "stu" :
#创建
sql = "create table if not exists dept(sid INT PRIMARY KEY,sname VARCHAR(50),age INT,gander VARCHAR(10),province VARCHAR(50),tuition INT)"
cursor.execute(sql)
else :
print u "输入错误的表名,表明为dept、emp、salgrade、stu..."
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[ 0 ], e.args[ 1 ])
def inser_onedata_table( self ,table):
try :
if table = = "dept" :
sql = "insert into dept values(%s,%s,%s)"
param = ( 40 , 'cai wu bu' , 'wu han' )
n = cursor.execute(sql,param)
print 'insert' ,n
elif table = = "emp" :
sql = "insert into emp values(%s,%s,%s,%s,%s,%s,%s,%s)"
param = ( 1009 , 'a niu' , 'dong shi zhang' , NULL, '2001-11-17' , 50000 , NULL, 10 )
n = cursor.execute(sql,param)
print 'insert' ,n
elif table = = "salgrade" :
sql = "insert into salgrade values(%s,%s,%s)"
param = ( 1 , 7000 , 12000 )
n = cursor.execute(sql,param)
print 'insert' ,n
elif table = = "stu" :
sql = "insert into stu values(%s,%s,%s,%s,%s,%s)"
param = ( '1' , '001' , '23' , 'nan' , 'bei jing' , '1500' )
n = cursor.execute(sql,param)
print 'insert' ,n
else :
print u "输入错误的表名,表明为dept、emp、salgrade、stu..."
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[ 0 ], e.args[ 1 ])
def inser_muldata_table( self ,table):
try :
if table = = "dept" :
sql = "insert into dept values(%s,%s,%s)"
param = (( 10 , 'jiao yan bu' , 'bei jing' ),( 20 , 'xue gong bu' , 'shang hai' ),( 30 , 'xiao shou bu' , 'guang zhou' ))
n = cursor.executemany(sql,param)
print 'insert' ,n
elif table = = "emp" :
sql = "insert into emp values(%s,%s,%s,%s,%s,%s,%s,%s)"
param = (( 1004 , 'liu bei' , 'jing li' , 1009 , '2001-04-02' , 29750 , NULL, 20 ),
( 1006 , 'guan yu' , 'jing li' , 1009 , '2001-05-01' , 28500 , NULL, 30 ),
( 1008 , 'zhu ge liang' , 'fen xi shi' , 1004 , '2007-04-19' , 30000 , NULL, 20 ),
( 1013 , 'pang' , 'fen xi shi' , 1004 , '2001-12-03' , 30000 , NULL, 20 ),
( 1002 , 'dai' , 'xiao shou yuan' , 1006 , '2001-02-20' , 16000 , 3000 , 30 ),
( 1003 , 'tian zheng' , 'xiao shou yuan' , 1006 , '2001-02-22' , 12500 , 5000 , 30 ),
( 1005 , 'xie xun' , 'xiao shou yuan' , 1006 , '2001-09-28' , 12500 , 14000 , 30 ),
( 1010 , 'wei yi xiao' , 'xiao shou yuan' , 1006 , '2001-09-08' , 15000 , 0 , 30 )
)
n = cursor.executemany(sql,param)
print 'insert' ,n
elif table = = "salgrade" :
sql = "insert into salgrade values(%s,%s,%s)"
param = (( 2 , 12010 , 14000 ),( 3 , 14010 , 20000 ),( 4 , 20010 , 30000 ),( 5 , 30010 , 99990 ))
n = cursor.executemany(sql,param)
print 'insert' ,n
elif table = = "stu" :
sql = "insert into stu values(%s,%s,%s,%s,%s,%s)"
param = ( ( '2' , '002' , '25' , 'nan' , 'liao ning' , '2500' ),
( '3' , '003' , '22' , 'nan' , 'bei jing' , '3500' ),
( '4' , '004' , '25' , 'nan' , 'bei jing' , '1500' ),
( '5' , '005' , '23' , 'nv' , 'bei jing' , '1000' ),
( '6' , '006' , '22' , 'nv' , 'shan dong' , '2500' ),
( '7' , '007' , '21' , 'nv' , 'bei jing' , '1600' ),
( '8' , '008' , '23' , 'nan' , 'bei jing' , '3500' ),
( '9' , '009' , '23' , 'nv' , 'guang zhou' , '2500' ),
( '10' , '010' , '18' , 'nan' , 'shan xi' , '3500' ),
( '11' , '011' , '23' , 'nan' , 'hu bei' , '4500' ),
( '12' , '011' , '24' , 'nan' , 'bei jing' , '1500' ),
( '13' , '011' , '24' , 'nan' , 'liao ning' , '2500' ),
( '14' , '011' , '22' , 'nan' , 'bei jing' , '3500' ),
( '15' , '011' , '25' , 'nan' , 'bei jing' , '1500' ),
( '16' , '011' , '23' , 'nv' , 'bei jing' , '1000' ),
( '17' , '011' , '22' , 'nv' , 'shan dong' , '2500' ),
( '18' , '011' , '21' , 'nv' , 'bei jing' , '1600' ),
( '19' , '011' , '23' , 'nan' , 'bei jing' , '3500' ),
( '20' , '011' , '23' , 'nv' , 'guang zhou' , '2500' ),
( '21' , '011' , '18' , 'nan' , 'shan xi' , '3500' ),
( '22' , '011' , '23' , 'nan' , 'hu bei' , '4500' ),
( '23' , '011' , '23' , 'nan' , 'bei jing' , '1500' ),
( '24' , '011' , '25' , 'nan' , 'liao ning' , '2500' ),
( '25' , '011' , '22' , 'nan' , 'bei jing' , '3500' ),
( '26' , '011' , '25' , 'nan' , 'bei jing' , '1500' ),
( '27' , '011' , '23' , 'nv' , 'bei jing' , '1000' ),
( '28' , '011' , '22' , 'nv' , 'shan dong' , '2500' ),
( '29' , '011' , '21' , 'nv' , 'bei jing' , '1600' ),
( '30' , '011' , '23' , 'nan' , 'bei jing' , '3500' ),
( '31' , '011' , '23' , 'nv' , 'guang zhou' , '2500' ),
( '32' , '011' , '18' , 'nan' , 'shan xi' , '3500' ),
( '33' , '033' , '23' , 'nan' , 'hu bei' , '4500' ),
( '34' , '034' , '23' , 'nan' , 'bei jing' , '1500' ),
( '35' , '035' , '25' , 'nan' , 'liao ning' , '2500' ),
( '36' , '036' , '22' , 'nan' , 'bei jing' , '3500' ),
( '37' , '037' , '25' , 'nan' , 'bei jing' , '1500' ),
( '38' , '038' , '23' , 'nv' , 'bei jing' , '1000' ),
( '39' , '039' , '22' , 'nv' , 'shan dong' , '2500' ),
( '40' , '040' , '21' , 'nv' , 'bei jing' , '1600' ),
( '41' , '041' , '23' , 'nan' , 'bei jing' , '3500' ),
( '42' , '042' , '23' , 'nv' , 'guang zhou' , '2500' ),
( '43' , '043' , '18' , 'nan' , 'shan xi' , '3500' ),
( '44' , '044' , '23' , 'nan' , 'hu bei' , '4500' ),
( '45' , '045' , '23' , 'nan' , 'bei jing' , '1500' ),
( '46' , '046' , '25' , 'nan' , 'liao ning' , '2500' ),
( '47' , '047' , '22' , 'nan' , 'bei jing' , '3500' ),
( '48' , '048' , '25' , 'nan' , 'bei jing' , '1500' ),
( '49' , '049' , '23' , 'nv' , 'bei jing' , '1000' ),
( '50' , '050' , '22' , 'nv' , 'shan dong' , '2500' ),
( '51' , '051' , '21' , 'nv' , 'bei jing' , '1600' ),
( '52' , '052' , '23' , 'nan' , 'bei jing' , '3500' ),
( '53' , '053' , '23' , 'nv' , 'guang zhou' , '2500' ),
( '54' , '054' , '18' , 'nan' , 'shan xi' , '3500' ),
( '55' , '055' , '23' , 'nan' , 'hu bei' , '4500' )
)
n = cursor.executemany(sql,param)
print 'insert' ,n
else :
print u "输入错误的表名,表明为dept、emp、salgrade、stu..."
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[ 0 ], e.args[ 1 ])
def update_table( self ,table,no,upno):
try :
if table = = "dept" :
#创建
sql = "update dept set deptno=%s where deptno=" + no
param = (upno)
n = cursor.execute(sql,param)
print 'update' ,n
elif table = = "emp" :
sql = "update emp set empno=%s where empno=" + no
param = (upno)
n = cursor.execute(sql,param)
print 'update' ,n
elif table = = "salgrade" :
sql = "update salgrade set grade=%s where grade=" + no
param = (upno)
n = cursor.execute(sql,param)
print 'update' ,n
elif table = = "stu" :
sql = "update stu set sname=%s where sname=" + no
param = (upno)
n = cursor.execute(sql,param)
print 'update' ,n
else :
print u "输入错误的表名,表明为dept、emp、salgrade、stu..."
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[ 0 ], e.args[ 1 ])
def query_data( self ,table):
try :
#查询
sql = "select * from " + table
n = cursor.execute(sql)
print cursor.fetchall()
for row in cursor.fetchall():
print row
for r in row:
print r
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[ 0 ], e.args[ 1 ])
def delete_data( self ,table,no)
try :
if table = = "dept" :
sql = "delete from dept where deptno=%s"
param = (upno)
n = cursor.execute(sql,param)
print 'delete' ,n
elif table = = "emp" :
sql = "delete from emp where empno=%s"
param = (upno)
n = cursor.execute(sql,param)
print 'delete' ,n
elif table = = "salgrade" :
sql = "delete from salgrade where grade=%s"
param = (upno)
n = cursor.execute(sql,param)
print 'delete' ,n
elif table = = "stu" :
sql = "delete from stu where sname=%s "
param = (upno)
n = cursor.execute(sql,param)
print 'delete' ,n
else :
print u "输入错误的表名,表明为dept、emp、salgrade、stu..."
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[ 0 ], e.args[ 1 ])
del down_db( self ):
try :
cursor.close()
#提交
conn.commit()
#关闭
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[ 0 ], e.args[ 1 ])
mysqlDB = Mysql_Oper( "127.0.0.1" , "root" , "root" , "exam" )
mysqlDB.db_connecet()
mysqlDB.drop_table( "dept" )
for table in [ "dept" , "emp" , "salgrade" , "stu" ]
mysqlDB.create_table(table)
mysqlDB.inser_onedata_table(table)
mysqlDB.inser_muldata_table(table)
mysqlDB.query_data(table)
mysqlDB.down_db()
|
后期我会把数据整合到CSV文件中,操作CSV文件对数据进行操作
希望本文所述对大家Python程序设计有所帮助。
原文链接:http://blog.csdn.net/henni_719/article/details/51863222