资讯专栏INFORMATION COLUMN

【Python3】pymysql模块

Drummor / 2087人阅读

摘要:模块什么是是在版本中用于连接服务器的一个库,中则使用。遵循数据库规范,并包含了客户端库。

【Python3】pymysql模块 1. 什么是 PyMySQL?

PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。

PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。

2. PyMySQL 安装

</>复制代码

  1. pip3 install PyMySQL
3. 使用操作

</>复制代码

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import pymysql
  4. # 创建连接
  5. conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="123", db="t1")
  6. # 创建游标
  7. cursor = conn.cursor()
  8. # 执行SQL,并返回收影响行数
  9. effect_row = cursor.execute("update hosts set host = "1.1.1.2"")
  10. # 执行SQL,并返回受影响行数
  11. #effect_row = cursor.execute("update hosts set host = "1.1.1.2" where nid > %s", (1,))
  12. # 执行SQL,并返回受影响行数
  13. #effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)])
  14. # 提交,不然无法保存新建或者修改的数据
  15. conn.commit()
  16. # 关闭游标
  17. cursor.close()
  18. # 关闭连接
  19. conn.close()

注:当使用字符串格式化时容易引起sql注入

</>复制代码

  1. sql = "select * from userinfo where username="%s" and password="%s" " %(user,pwd,) #不推荐使用

</>复制代码

  1. sql = "select * from userinfo where username=%s and password=%s",[user,pwd] #推荐使用,mysql会自动格式化,避免SQL注入

</>复制代码

  1. import pymysql
  2. user = input("请输入用户名:")
  3. pwd = input("请输入密码:")
  4. # 获取数据
  5. conn = pymysql.Connect(host="192.168.12.89",port=3306,user="root",password="123",database="s17day11db",charset="utf8")
  6. cursor = conn.cursor()
  7. v = cursor.execute("select * from userinfo where username=%s and password=%s",[user,pwd])
  8. result = cursor.fetchone()
  9. cursor.close()
  10. conn.close()
  11. print(result)

获取新创建数据自增ID

</>复制代码

  1. new_class_id = cursor.lastrowid # 获取新增数据自增ID
  2. 复制代码
  3. import pymysql
  4. # 获取数据
  5. conn = pymysql.Connect(host="192.168.12.89",port=3306,user="root",password="123",database="s17day11db",charset="utf8")
  6. cursor = conn.cursor()
  7. cursor.execute("insert into class(caption) values(%s)",["新班级"])
  8. conn.commit()
  9. new_class_id = cursor.lastrowid # 获取新增数据自增ID
  10. cursor.execute("insert into student(sname,gender,class_id) values(%s,%s,%s)",["李杰","男",new_class_id])
  11. conn.commit()
  12. cursor.close()
  13. conn.close()

查询语句

</>复制代码

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import pymysql
  4. conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="123", db="t1")
  5. cursor = conn.cursor()
  6. cursor.execute("select * from hosts")
  7. # 获取第一行数据
  8. row_1 = cursor.fetchone()
  9. # 获取前n行数据
  10. # row_2 = cursor.fetchmany(3)
  11. # 获取所有数据
  12. # row_3 = cursor.fetchall()
  13. conn.commit()
  14. cursor.close()
  15. conn.close()

注:
在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:

cursor.scroll(1,mode="relative") # 相对当前位置移动

cursor.scroll(2,mode="absolute") # 相对绝对位置移动

更改fetch数据类型
默认获取的数据是元祖类型,如果想要或者字典类型的数据

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

</>复制代码

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import pymysql
  4. conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="123", db="t1")
  5. # 游标设置为字典类型
  6. cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
  7. v = cursor.execute("SELECT * FROM score")
  8. result = cursor.fetchall()
  9. # result = cursor.fetchone()
  10. # result = cursor.fetchmany()
  11. print(result)
  12. cursor.close()
  13. conn.close()

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/40669.html

相关文章

  • Python3网络爬虫实战---5、存储库的安装:PyMySQL、PyMongo、RedisPy、R

    摘要:相关链接官方文档安装推荐使用安装,命令如下运行完毕之后即可完成的安装。上一篇文章网络爬虫实战数据库的安装下一篇文章网络爬虫实战库的安装 上一篇文章:Python3网络爬虫实战---4、数据库的安装:MySQL、MongoDB、Redis下一篇文章:Python3网络爬虫实战---6、Web库的安装:Flask、Tornado 在前面一节我们介绍了几个数据库的安装方式,但这仅仅是用来存...

    AlanKeene 评论0 收藏0
  • 如何用python的pymysql操作MySQL数据库?

    摘要:一介绍是在版本中用于连接和操作服务器的一个库引入方式二连接数据库的完整流程引入模块引入第三方库创建连接对象用户名密码端口号默认为且此处为整数类型数据库名连接地址使用连接对象创建游标对象游标对象是通过链接对象进行创 ...

    Keagan 评论0 收藏0
  • 利用Django开发个小型商城(一)

    摘要:利用开发个小型商城我们本期的教程是教大家如何利用开发一个小型的商城这里所说的小型商城只是功能上的简朴。并于年月在许可证下发布。这套框架是以比利时的吉普赛爵士吉他手来命名的。是重量级选手中最有代表性的一位。 利用Django开发个小型商城 我们本期的教程是教大家如何利用Django开发一个小型的商城,这里所说的小型商城只是功能上的简朴。 作者:黄志成(小黄) 作者博客:博客地址 前提 1...

    RobinTang 评论0 收藏0
  • 使用flask开发api——部署flask,使用gunicorn+gevent模式的http ser

    摘要:使用开发部署,使用模式的用开发了服务端的,记录部署上服务器的过程,以供后续使用。退出虚拟环境如果服务器中没有安装,先进行安装增加配置文件创建配置文件编辑内容如下更新会提示相关的进程已经被加入要关闭相关的进程可以用开启可以用 使用flask开发api——部署flask,使用gunicorn+gevent模式的http server 用flask开发了服务端的api,记录部署上服务器的过程...

    XboxYan 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<