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,或者扫描下方二维码,关注公众号,即可获取最新文章。

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

相关推荐
程序员清风4 分钟前
北京回长沙了,简单谈谈感受!
java·后端·面试
何中应13 分钟前
请求头设置没有生效
java·后端
NPE~16 分钟前
自动化工具Drissonpage 保姆级教程(含xpath语法)
运维·后端·爬虫·自动化·网络爬虫·xpath·浏览器自动化
极客小云17 分钟前
【ComfyUI API 自动化利器:comfyui_xy Python 库使用详解】
网络·python·自动化·comfyui
闲人编程33 分钟前
Elasticsearch搜索引擎集成指南
python·elasticsearch·搜索引擎·jenkins·索引·副本·分片
痴儿哈哈42 分钟前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
宋小黑1 小时前
JDK 6到25 全版本网盘合集 (Windows + Mac + Linux)
java·后端
花酒锄作田1 小时前
SQLAlchemy中使用UPSERT
python·sqlalchemy
SoleMotive.1 小时前
一个准程序员的健身日志:用算法调试我的增肌计划
python·程序员·健身·职业转型
念何架构之路1 小时前
Go进阶之panic
开发语言·后端·golang