Python实现连接操作MySql数据库

65 篇文章 4 订阅
36 篇文章 3 订阅


 

        在本文中介绍 Python3 使用PyMySQL连接数据库,并实现简单的增删改查。(注意是python3)

 

1、安装PyMySQL

 

        PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装。

① 使用pip命令安装

 

[plain] view plain copy

  1. pip install PyMySQL  

② 如果你的系统不支持 pip 命令,可以使用以下git方式安装

 

[plain] view plain copy

  1. //使用git下载安装包  
  2. $ git clone https://github.com/PyMySQL/PyMySQL  
  3. $ cd PyMySQL/  
  4. $ python3 setup.py install  

 

2、Python连接MySql数据库

 

连接数据库前,请先确认以下事项:

        Ⅰ 在你的机子上已经安装了 Python MySQLdb 模块。
        Ⅱ 您已经创建了数据库 test
        Ⅲ 连接数据库test使用的用户名为 root,密码为 root,你可以可以自己设定或者直接使用root用户名及其密码。

 

[python] view plain copy

  1. # *===================================*  
  2. # * Created by Zhihua_w.  
  3. # * Author: Wei ZhiHua  
  4. # * Date: 2017/1/10 0003  
  5. # * Time: 下午 2:28  
  6. # * Project: PYTHON STUDY  
  7. # * Power: DATABASE  
  8. # *===================================*  
  9.   
  10. import pymysql  
  11.   
  12. # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  
  13. db = pymysql.connect("localhost""root""root""test")  
  14. # 使用 cursor() 方法创建一个游标对象 cursor  
  15. cursor = db.cursor()  
  16.   
  17. # 使用 execute()  方法执行 SQL 查询  
  18. cursor.execute("SELECT VERSION()")  
  19. # 使用 fetchone() 方法获取单条数据.  
  20. data = cursor.fetchone()  
  21. print("Database version : %s " % data)  
  22.   
  23. # 关闭数据库连接  
  24. db.close()  

 

3、Python操作MySql数据库实现增删改查

 

① 数据库插入操作

 

[python] view plain copy

  1. # *===================================*  
  2. # * Created by Zhihua_w.  
  3. # * Author: Wei ZhiHua  
  4. # * Date: 2017/1/10 0004  
  5. # * Time: 下午 2:32  
  6. # * Project: PYTHON STUDY  
  7. # * Power: DATABASE  
  8. # *===================================*  
  9.   
  10. import pymysql  
  11.   
  12. # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  
  13. db = pymysql.connect("localhost""root""root""test")  
  14. # 使用 cursor() 方法创建一个游标对象 cursor  
  15. cursor = db.cursor()  
  16.   
  17. # SQL 插入语句  
  18. sql = """INSERT INTO user(name) 
  19.          VALUES ('Mac')"""  
  20. try:  
  21.    # 执行sql语句  
  22.    cursor.execute(sql)  
  23.    # 提交到数据库执行  
  24.    db.commit()  
  25. except:  
  26.    # 如果发生错误则回滚  
  27.    db.rollback()  
  28.   
  29. # 关闭数据库连接  
  30. db.close()  

② 数据库查询

 

[python] view plain copy

  1. # *===================================*  
  2. # * Created by Zhihua_w.  
  3. # * Author: Wei ZhiHua  
  4. # * Date: 2017/1/10 0005  
  5. # * Time: 下午 2:39  
  6. # * Project: PYTHON STUDY  
  7. # * Power: DATABASE  
  8. # *===================================*  
  9.   
  10. import pymysql  
  11.   
  12. # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  
  13. db = pymysql.connect("localhost""root""root""test")  
  14. # 使用 cursor() 方法创建一个游标对象 cursor  
  15. cursor = db.cursor()  
  16.   
  17. # SQL 查询语句  
  18. sql = "SELECT * FROM user"  
  19.   
  20. try:  
  21.     # 执行SQL语句  
  22.     cursor.execute(sql)  
  23.     # 获取所有记录列表  
  24.     results = cursor.fetchall()  
  25.     for row in results:  
  26.         id = row[0]  
  27.         name = row[1]  
  28.         # 打印结果  
  29.         print("id=%s,name=%s" % \  
  30.               (id, name))  
  31. except:  
  32.     print("Error: unable to fecth data")  
  33.   
  34. # 关闭数据库连接  
  35. db.close()  

③ 数据库更新

 

[php] view plain copy

  1. # *===================================*  
  2. # * Created by Zhihua_w.  
  3. # * Author: Wei ZhiHua  
  4. # * Date: 2017/1/10 0005  
  5. # * Time: 下午 2:39  
  6. # * Project: PYTHON STUDY  
  7. # * Power: DATABASE  
  8. # *===================================*  
  9.   
  10. import pymysql  
  11.   
  12. # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  
  13. db = pymysql.connect("localhost""root""root""test")  
  14. # 使用 cursor() 方法创建一个游标对象 cursor  
  15. cursor = db.cursor()  
  16.   
  17. # SQL 更新语句  
  18. sql = "UPDATE user SET name = 'Bob' WHERE id = 1"  
  19. try:  
  20.     # 执行SQL语句  
  21.     cursor.execute(sql)  
  22.     # 提交到数据库执行  
  23.     db.commit()  
  24. except:  
  25.     # 发生错误时回滚  
  26.     db.rollback()  
  27.      
  28. # 关闭数据库连接  
  29. db.close()  

④ 数据库删除

 

[php] view plain copy

  1. # *===================================*  
  2. # * Created by Zhihua_w.  
  3. # * Author: Wei ZhiHua  
  4. # * Date: 2017/1/10 0006  
  5. # * Time: 下午 2:49  
  6. # * Project: PYTHON STUDY  
  7. # * Power: DATABASE  
  8. # *===================================*  
  9.   
  10. import pymysql  
  11.   
  12. # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  
  13. db = pymysql.connect("localhost""root""root""test")  
  14. # 使用 cursor() 方法创建一个游标对象 cursor  
  15. cursor = db.cursor()  
  16.   
  17. # SQL 删除语句  
  18. sql = "DELETE FROM user WHERE id  = 1"  
  19. try:  
  20.     # 执行SQL语句  
  21.     cursor.execute(sql)  
  22.     # 提交修改  
  23.     db.commit()  
  24. except:  
  25.     # 发生错误时回滚  
  26.     db.rollback()  
  27.   
  28. # 关闭数据库连接  
  29. db.close()

 

转自:http://blog.csdn.net/zhihua_w https://blog.csdn.net/Zhihua_W/article/details/54313258

  • 8
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值