python操作mysql(4)--增删改查

1.新增数据库表:

 1 我们可以使用execute()方法来为数据库创建表,如下所示创建表EMPLOYEE:
 2 
 3 程序实现:
 4 
 5 import pymysql
 6 
 7  
 8 
 9 # 打开数据库连接
10 
11 db = pymysql.connect(host="localhost",port=3306,user="root", password="root",db= "test",charset='utf8')
12 
13  
14 
15 # 使用cursor()方法获取操作游标
16 
17 cursor = db.cursor()
18 
19  
20 
21 # 创建数据表SQL语句
22 
23 sql = """CREATE TABLE EMPLOYEE (
24 
25          FIRST_NAME  CHAR(20) NOT NULL,
26 
27          LAST_NAME  CHAR(20),
28 
29          AGE INT,
30 
31          SEX CHAR(1),
32 
33          INCOME FLOAT )"""
34 
35 try:
36 
37    # 执行sql语句
38 
39     cursor.execute(sql)
40 
41    # 提交到数据库执行
42 
43     db.commit()
44 
45 except:
46 
47    # 如果发生错误则回滚
48 
49     db.rollback()
50 
51  
52 
53 # 关闭数据库连接
54 
55 db.close()

2.给表中增加数据:

 1 方式一:
 2 
 3 import pymysql
 4 
 5  
 6 
 7 # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)
 8 
 9 db = pymysql.connect(host="localhost",port=3306,user="root", password="root",db= "test",charset='utf8')
10 
11 # 使用 cursor() 方法创建一个游标对象 cursor
12 
13 cursor = db.cursor()
14 
15  
16 
17 # SQL 插入语句
18 
19 sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
20 
21          LAST_NAME, AGE, SEX, INCOME)
22 
23          VALUES ('xiaoneng', 'xn', 20, 'M', 3000)"""
24 
25 try:
26 
27    # 执行sql语句
28 
29    cursor.execute(sql)
30 
31    # 提交到数据库执行
32 
33    db.commit()
34 
35 except:
36 
37    # Rollback in case there is any error
38 
39    db.rollback()
40 
41  
42 
43 # 关闭数据库连接
44 
45 db.close()
 1 方式二:
 2 
 3 import pymysql
 4 
 5  
 6 
 7 # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)
 8 
 9 db = pymysql.connect(host="localhost",port=3306,user="root", password="root",db= "test",charset='utf8' )
10 
11 # 使用 cursor() 方法创建一个游标对象 cursor
12 
13 cursor = db.cursor()
14 
15  
16 
17 # SQL 插入语句
18 
19 sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \
20 
21        LAST_NAME, AGE, SEX, INCOME) \
22 
23        VALUES ('%s', '%s', '%d', '%c', '%d' )" % \
24 
25        ('李四', '小四', 22, 'M', 15000)
26 
27  
28 
29 try:
30 
31    # 执行sql语句
32 
33    cursor.execute(sql)
34 
35    # 提交到数据库执行
36 
37    db.commit()
38 
39 except:
40 
41    # Rollback in case there is any error
42 
43    db.rollback()
44 
45  
46 
47 # 关闭数据库连接
48 
49 db.close()

3.查询数据:

Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。

fetchone(): 该方法获取下一个查询结果集。结果集是一个对象

fetchall():接收全部的返回结果行.

rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。

 1 查询EMPLOYEE表中salary(工资)字段大于1000的所有数据:
 2 
 3 import pymysql
 4 
 5  
 6 
 7 # 打开数据库连接
 8 
 9 db = pymysql.connect(host="localhost",port=3306,user="root", password="root",db= "test",charset='utf8' )
10 
11  
12 
13 # 使用cursor()方法获取操作游标
14 
15 cursor = db.cursor()
16 
17  
18 
19 # SQL 查询语句
20 
21 sql = "SELECT * FROM EMPLOYEE \
22 
23        WHERE INCOME > '%d'" % (1000)
24 
25 try:
26 
27    # 执行SQL语句
28 
29    cursor.execute(sql)
30 
31    # 获取所有记录列表
32 
33    results = cursor.fetchall()
34 
35    for row in results:
36 
37       fname = row[0]
38 
39       lname = row[1]
40 
41       age = row[2]
42 
43       sex = row[3]
44 
45       income = row[4]
46 
47       # 打印结果
48 
49       print("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
50 
51              (fname, lname, age, sex, income ))
52 
53 except:
54 
55    print("Error: unable to fecth data")
56 
57  
58 
59 # 关闭数据库连接
60 
61 db.close()

4.更新操作:

更新操作用于更新数据表的的数据,以下实例将 EMPLOYEE 表中的FIRST_NAME = '李四',INCOME字段的值15000改为25000

 1 import pymysql
 2 
 3  
 4 
 5 # 打开数据库连接
 6 
 7 db = pymysql.connect(host="localhost",port=3306,user="root", password="root",db= "test",charset='utf8' )
 8 
 9  
10 
11 # 使用cursor()方法获取操作游标
12 
13 cursor = db.cursor()
14 
15  
16 
17 # SQL 更新语句
18 
19 sql = "UPDATE EMPLOYEE SET INCOME = 25000 WHERE FIRST_NAME = '李四'"
20 
21 try:
22 
23    # 执行SQL语句
24 
25    cursor.execute(sql)
26 
27    # 提交到数据库执行
28 
29    db.commit()
30 
31 except:
32 
33    # 发生错误时回滚
34 
35    db.rollback()
36 
37  
38 
39 # 关闭数据库连接
40 
41 db.close()

5.删除操作:

删除操作用于删除数据表中的数据,以下实例演示了删除数据表 EMPLOYEE 中 AGE 大于 20 的所有数据:

 1 import pymysql
 2 
 3  
 4 
 5 # 打开数据库连接
 6 
 7 db = pymysql.connect(host="localhost",port=3306,user="root", password="root",db= "test",charset='utf8')
 8 
 9  
10 
11 # 使用cursor()方法获取操作游标
12 
13 cursor = db.cursor()
14 
15  
16 
17 # SQL 删除语句
18 
19 sql = "DELETE FROM EMPLOYEE WHERE FIRST_NAME = '李四'"
20 
21  
22 
23 try:
24 
25    # 执行SQL语句
26 
27    cursor.execute(sql)
28 
29    # 提交到数据库执行
30 
31    db.commit()
32 
33 except:
34 
35    # 发生错误时回滚
36 
37    db.rollback()
38 
39  
40 
41 # 关闭数据库连接
42 
43 db.close()
posted @ 2018-11-07 09:50  jaysonzxk  阅读(342)  评论(0编辑  收藏  举报