Python实现连接操作MySql数据库

xiaoxiao2021-02-28  23

    在本文中介绍 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 pip install PyMySQL  

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

[plain]  view plain  copy //使用git下载安装包  $ git clone https://github.com/PyMySQL/PyMySQL  $ cd PyMySQL/  $ python3 setup.py install  

2、Python连接MySql数据库

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

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

[python]  view plain  copy # *===================================*  # * Created by Zhihua_w.  # * Author: Wei ZhiHua  # * Date: 2017/1/10 0003  # * Time: 下午 2:28  # * Project: PYTHON STUDY  # * Power: DATABASE  # *===================================*    import pymysql    # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  db = pymysql.connect("localhost""root""root""test")  # 使用 cursor() 方法创建一个游标对象 cursor  cursor = db.cursor()    # 使用 execute()  方法执行 SQL 查询  cursor.execute("SELECT VERSION()")  # 使用 fetchone() 方法获取单条数据.  data = cursor.fetchone()  print("Database version : %s " % data)    # 关闭数据库连接  db.close()  

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

① 数据库插入操作

[python]  view plain  copy # *===================================*  # * Created by Zhihua_w.  # * Author: Wei ZhiHua  # * Date: 2017/1/10 0004  # * Time: 下午 2:32  # * Project: PYTHON STUDY  # * Power: DATABASE  # *===================================*    import pymysql    # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  db = pymysql.connect("localhost""root""root""test")  # 使用 cursor() 方法创建一个游标对象 cursor  cursor = db.cursor()    # SQL 插入语句  sql = """INSERT INTO user(name)          VALUES ('Mac')"""  try:     # 执行sql语句     cursor.execute(sql)     # 提交到数据库执行     db.commit()  except:     # 如果发生错误则回滚     db.rollback()    # 关闭数据库连接  db.close()  

② 数据库查询

[python]  view plain  copy # *===================================*  # * Created by Zhihua_w.  # * Author: Wei ZhiHua  # * Date: 2017/1/10 0005  # * Time: 下午 2:39  # * Project: PYTHON STUDY  # * Power: DATABASE  # *===================================*    import pymysql    # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  db = pymysql.connect("localhost""root""root""test")  # 使用 cursor() 方法创建一个游标对象 cursor  cursor = db.cursor()    # SQL 查询语句  sql = "SELECT * FROM user"    try:      # 执行SQL语句      cursor.execute(sql)      # 获取所有记录列表      results = cursor.fetchall()      for row in results:          id = row[0]          name = row[1]          # 打印结果          print("id=%s,name=%s" % \                (id, name))  except:      print("Error: unable to fecth data")    # 关闭数据库连接  db.close()  

③ 数据库更新

[php]  view plain  copy # *===================================*  # * Created by Zhihua_w.  # * Author: Wei ZhiHua  # * Date: 2017/1/10 0005  # * Time: 下午 2:39  # * Project: PYTHON STUDY  # * Power: DATABASE  # *===================================*    import pymysql    # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  db = pymysql.connect("localhost""root""root""test")  # 使用 cursor() 方法创建一个游标对象 cursor  cursor = db.cursor()    # SQL 更新语句  sql = "UPDATE user SET name = 'Bob' WHERE id = 1"  try:      # 执行SQL语句      cursor.execute(sql)      # 提交到数据库执行      db.commit()  except:      # 发生错误时回滚      db.rollback()       # 关闭数据库连接  db.close()  

④ 数据库删除

[php]  view plain  copy # *===================================*  # * Created by Zhihua_w.  # * Author: Wei ZhiHua  # * Date: 2017/1/10 0006  # * Time: 下午 2:49  # * Project: PYTHON STUDY  # * Power: DATABASE  # *===================================*    import pymysql    # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)  db = pymysql.connect("localhost""root""root""test")  # 使用 cursor() 方法创建一个游标对象 cursor  cursor = db.cursor()    # SQL 删除语句  sql = "DELETE FROM user WHERE id  = 1"  try:      # 执行SQL语句      cursor.execute(sql)      # 提交修改      db.commit()  except:      # 发生错误时回滚      db.rollback()    # 关闭数据库连接  db.close()  
转载请注明原文地址: https://www.6miu.com/read-2100352.html

最新回复(0)