Python MySQL 数据库查询:选择数据、使用筛选条件、防止 SQL 注入

从表格中选择数据

要从MySQL中的表格中选择数据,请使用"SELECT"语句:

示例选择"customers"表格中的所有记录,并显示结果:

python 复制代码
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

注意 :我们使用 fetchall() 方法,该方法从上次执行的语句中获取所有行。

选择列

要仅选择表格中的某些列,请使用"SELECT"语句,后跟列名:

示例仅选择name和address列:

python 复制代码
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT name, address FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

使用 fetchone() 方法

如果您只对一行数据感兴趣,可以使用 fetchone() 方法。

fetchone() 方法将返回结果的第一行:

示例仅获取一行:

python 复制代码
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchone()

print(myresult)

这是您提供的内容的Markdown排版,按照您的要求进行了整理。如果需要进一步的编辑或修改,请告诉我。

使用筛选条件选择记录

在从表格中选择记录时,您可以使用"WHERE"语句来筛选选择的记录:

示例选择地址为"Park Lane 38"的记录:

python 复制代码
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers WHERE address ='Park Lane 38'"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

通配符字符

您还可以选择以给定字母或短语开头、包含或以给定字母或短语结尾的记录。

使用 % 来表示通配符字符:

示例选择地址中包含单词 "way" 的记录:

python 复制代码
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers WHERE address LIKE '%way%'"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

防止SQL注入

当查询值由用户提供时,应该转义这些值。

这是为了防止SQL注入,这是一种常见的网络黑客技术,可以破坏或滥用您的数据库。

mysql.connector 模块具有转义查询值的方法:

示例使用占位符 %s 方法转义查询值:

python 复制代码
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "SELECT * FROM customers WHERE address = %s"
adr = ("Yellow Garden 2", )

mycursor.execute(sql, adr)

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

最后

为了方便其他设备和平台的小伙伴观看往期文章:公众号搜索Let us Coding,或者扫描下方二维码,关注公众号,即可获取最新文章。

看完如果觉得有帮助,欢迎点赞、收藏 和关注

相关推荐
FengY_HYY15 分钟前
从人类组织到人类&大模型组织,我的位置在哪里?
人工智能·程序员·llm
2601_9679801129 分钟前
Flask Web 应用
python·flask·web开发·环境变量·虚拟环境
锋行天下1 小时前
langgraph全栈项目(一)
前端·人工智能·后端
aifannihao1 小时前
ß 大写变 SS:用 Python 算清 Ruby2Shell 的缓冲区账本
python·安全·标准库·缓冲区溢出·字符串编码
学长毕设1 小时前
基于SpringBoot的游戏攻略社区交流系统-附源码
spring boot·后端·游戏
程序员cxuan1 小时前
不得了,Google 造反了,Opus 5.5 又被干了
人工智能·后端·程序员
liwulin05061 小时前
【OLLAMA】调用SKILL的通用ReAct
python
Csvn2 小时前
Nginx 缓存体系:三层 key、七态 X-Cache-Status 与锁/stale 策略
后端·nginx
郝学胜-神的一滴2 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
时间海8713 小时前
前端表单如何防止重复提交
后端