python 通过 pymysql模块 操作 mysql 数据库
Python 中操作 MySQL 步骤
安装模块
pip install pymysql
引入模块
在py文件中引入pymysql模块
from pymysql import *
Connection 对象
-
用于建立与数据库的连接
-
创建对象:调用connect()方法
conn=connect(参数列表)
- 参数host:连接的mysql主机,如果本机是'localhost'
- 参数port:连接的mysql主机的端口,默认是3306
- 参数database:数据库的名称
- 参数user:连接的用户名
- 参数password:连接的密码
- 参数charset:通信采用的编码方式,推荐使用utf8
对象的方法
- close()关闭连接
- commit()提交
- cursor()返回Cursor对象,用于执行sql语句并获得结果
Cursor对象
- 用于执行sql语句,使用频度最高的语句为select、insert、update、delete
- 获取Cursor对象:调用Connection对象的cursor()方法
cs1=conn.cursor()
对象的方法
- close()关闭
- execute(operation [, parameters ])执行语句,返回受影响的行数,主要用于执行insert、update、delete语句,也可以执行create、alter、drop等语句
- fetchone()执行查询语句时,获取查询结果集的第一个行数据,返回一个元组
- fetchall()执行查询时,获取结果集的所有行,一行构成一个元组,再将这些元组装入一个元组返回
对象的属性
- rowcount只读属性,表示最近一次execute()执行后受影响的行数
- connection获得当前连接对象
使用示例,通过pymysql实现对数据库的增删改查
曾删改的示例
from pymysql import * def main(): # 创建Connection连接 conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='mysql',charset='utf8') # 获得Cursor对象 cur_obj = conn.cursor() # 执行insert语句,增加一条数据,并返回受影响的行数: count = cur_obj.execute('insert into goods_cates(name) values("硬盘")') # execute 就是执行的意思 #打印受影响的行数 print(count) count = cur_obj.execute('insert into goods_cates(name) values("光盘")') print(count) # 更新 count = cur_obj.execute('update goods_cates set name="机械硬盘" where name="硬盘"')
# 删除 count = cur_obj.execute('delete from goods_cates where id=6') # 提交之前的操作,如果之前已经之执行过多次的execute,那么就都进行提交,只有进行的提交之后,所有的修改才会生效 conn.commit() # 关闭Cursor对象,先关闭操作对象,再关闭连接对象,必须两者都进行关闭 cur_obj.close() # 关闭Connection对象 conn.close() if __name__ == '__main__': main()
查询示例
查询一行数据
from pymysql import * def main(): # 创建Connection连接 conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8') # 获得Cursor对象 cs1 = conn.cursor() # 执行select语句,并返回受影响的行数:查询一条数据 count = cs1.execute('select id,name from goods where id>=4') # 打印受影响的行数 print("查询到%d条数据:" % count) for i in range(count): # 获取查询的结果,只能查询到第一行数据 result = cs1.fetchone() # 打印查询的结果 print(result) # 关闭Cursor对象 cs1.close() conn.close() if __name__ == '__main__': main()
查询多行数据
from pymysql import * def main(): # 创建Connection连接 conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8') # 获得Cursor对象 cs1 = conn.cursor() # 执行select语句,并返回受影响的行数:查询一条数据 count = cs1.execute('select id,name from goods where id>=4') # 打印受影响的行数 print("查询到%d条数据:" % count) # for i in range(count): # # 获取查询的结果 # result = cs1.fetchone() # # 打印查询的结果 # print(result) # # 获取查询的结果 result = cs1.fetchall() print(result) # 关闭Cursor对象 cs1.close() conn.close() if __name__ == '__main__': main()
参数化,防止sql注入.在python web框架 django中,它自带的orm已经考虑到了sql注入的问题,但是在pymysql中是手动输入sql语句进行查询,所以考虑防止sql注入是必须的.
- sql语句的参数化,可以有效防止sql注入
- 注意:此处不同于python的字符串格式化
from pymysql import * def main(): find_name = input("请输入物品名称:") # 创建Connection连接 conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8') # 获得Cursor对象 cs1 = conn.cursor() # # 非安全的方式 # # 输入 " or 1=1 or " (双引号也要输入) # sql = 'select * from goods where name="%s"' % find_name # print("""sql===>%s<====""" % sql) # # 执行select语句,并返回受影响的行数:查询所有数据 # count = cs1.execute(sql) # 安全的方式 # 构造参数列表 params = [find_name] # 执行select语句,并返回受影响的行数:查询所有数据 count = cs1.execute('select * from goods where name=%s', params) # 打印受影响的行数 print(count) # 获取查询的结果 # result = cs1.fetchone() result = cs1.fetchall() # 打印查询的结果 print(result) # 关闭Cursor对象 cs1.close() # 关闭Connection对象 conn.close() if __name__ == '__main__': main()
当我仰望星空, 看见了涛涛江水, 闻到了人声鼎沸;可当我蓦然回望,再也触摸不到那逝去的时光,再也看不到那夕阳下的少年!
分类:
Database_NOTES
, 嘿python基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗