代码改变世界

pyrhon3与mysql:查、更、删49

2018-07-03 14:40  改改~_~  阅读(341)  评论(0编辑  收藏  举报
 1 import pymysql
 2 
 3 conn = pymysql.connect(host='localhost',user='root',passwd='123456',db='jodb1',port=3307,charset='utf8')
 4 # 172.31.10.225
 5 
 6 # 查询EMPLOYEE表中salary(工资)字段大于1000的所有数据:
 7 cursor = conn.cursor()
 8 
 9 
10 print('1---------------------------------------------------------------------')
11 sql = 'select * from employee where income > 2500'
12 cursor.execute(sql)
13 results = cursor.fetchall()
14 try:
15     for row in results:
16         fname = row[0]
17         lname = row[1]
18         age = row[2]
19         sex = row[3]
20         income = row[4]
21         print('first_name={0},last_name={1},age={2},sex={3},income={4}'.format(fname,lname,age,sex,income))
22 
23 except:
24     print('oh,there a exception!')
25 conn.close()
26 print('select successfully')
27 
28 
29 
30 print('2---------------------------------------------------------------------')
31 
32 # sql_update = 'update employee set age = 28 where sex ={0}'.format('F')
33 sql2 = "update employee set age = 28 where sex = '%c'"%('M')
34 try:
35     cursor.execute(sql2)
36     conn.commit()
37 except:
38     conn.rollback()
39     print('Execute this statement')
40 
41 
42 print('3---------------------------------------------------------------------')
43 
44 sql3 = "delete from employee where income = '%d'"%(3900)
45 try:
46     cursor.execute(sql3)
47     conn.commit()
48 except:
49     conn.rollback()

执行其中一部分时将其他部分删除,否则会有错误