1、数据库的安装和连接

 1 #数据库安装
 2 pip install PyMySQL
 3 
 4 #数据库操作
 5 import pymysql
 6 
 7 db = pymysql.connect("数据库ip","用户","密码","数据库" ) # 打开数据库连接
 8 cursor.execute("SELECT VERSION()")                    # 使用 execute() 方法执行 SQL 查询
 9 data = cursor.fetchone()                              # 使用 fetchone() 方法获取单条数据
10 print ("Database version : %s " % data)
11 db.close()                                            # 关闭数据库连接
12 
13 import pymysql
14 
15 conn = pymysql.connect(
16         host='localhost', user='root', password="root",
17         database='db', port=3306, charset='utf-8',
18 )
19 
20 cur = conn.cursor(cursor=pymysql.cursors.DictCursor)

2、创建表操作

 1 import pymysql
 2  
 3 # 打开数据库连接
 4 db = pymysql.connect("localhost","testuser","test123","TESTDB" )
 5  
 6 # 使用cursor()方法获取操作游标 
 7 cursor = db.cursor()
 8  
 9 # SQL 插入语句
10 sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
11          LAST_NAME, AGE, SEX, INCOME)
12          VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
13 try:
14    cursor.execute(sql) # 执行sql语句
15    db.commit()         # 提交到数据库执行
16 except:
17    db.rollback()       # 如果发生错误则回滚
18  
19 # 关闭数据库连接
20 db.close()
21 
22 import pymysql
23  
24 # 打开数据库连接
25 db = pymysql.connect("localhost","testuser","test123","TESTDB" )
26  
27 # 使用cursor()方法获取操作游标 
28 cursor = db.cursor()
29  
30 # SQL 插入语句
31 sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \
32        LAST_NAME, AGE, SEX, INCOME) \
33        VALUES (%s, %s,  %s,  %s,  %s )" % \
34        ('Mac', 'Mohan', 20, 'M', 2000)
35 try:
36    
37    cursor.execute(sql)  # 执行sql语句
38    db.commit()          # 执行sql语句
39 except:
40    db.rollback()        # 发生错误时回滚
41  
42 # 关闭数据库连接
43 db.close()
44 
45 另一种形式

 

3、查询操作

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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
  • fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
  • fetchall(): 接收全部的返回结果行.
  • rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。
 1 import pymysql
 2  
 3 # 打开数据库连接
 4 db = pymysql.connect("localhost","testuser","test123","TESTDB" )
 5  
 6 # 使用cursor()方法获取操作游标 
 7 cursor = db.cursor()
 8  
 9 # SQL 查询语句
10 sql = "SELECT * FROM EMPLOYEE \
11        WHERE INCOME > %s" % (1000)
12 try:
13    
14    cursor.execute(sql)# 执行SQL语句
15    results = cursor.fetchall()# 获取所有记录列表
16    for row in results:
17       fname = row[0]
18       lname = row[1]
19       age = row[2]
20       sex = row[3]
21       income = row[4]
22        # 打印结果
23       print ("fname=%s,lname=%s,age=%s,sex=%s,income=%s" % \
24              (fname, lname, age, sex, income ))
25 except:
26    print ("Error: unable to fetch data")
27  
28 # 关闭数据库连接
29 db.close()

 4、更新操作

 1 import pymysql
 2  
 3 # 打开数据库连接
 4 db = pymysql.connect("localhost","testuser","test123","TESTDB" )
 5  
 6 # 使用cursor()方法获取操作游标 
 7 cursor = db.cursor()
 8  
 9 # SQL 更新语句
10 sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
11 try:
12    cursor.execute(sql)  # 执行SQL语句
13    db.commit()          # 提交到数据库执行
14 except
15    db.rollback()        # 发生错误时回滚
16  
17 # 关闭数据库连接
18 db.close()

 5、删除操作

 1 import pymysql
 2  
 3 # 打开数据库连接
 4 db = pymysql.connect("localhost","testuser","test123","TESTDB" )
 5  
 6 # 使用cursor()方法获取操作游标 
 7 cursor = db.cursor()
 8  
 9 # SQL 删除语句
10 sql = "DELETE FROM EMPLOYEE WHERE AGE > %s" % (20)
11 try
12    cursor.execute(sql)  # 执行SQL语句
13    db.commit()          # 提交修改
14 except
15    db.rollback()        # 发生错误时回滚# 关闭连接
16 db.close()

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄