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

小万哥
• 阅读 96

从表格中选择数据

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

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

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列:

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() 方法将返回结果的第一行:

示例仅获取一行:

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"的记录:

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" 的记录:

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 方法转义查询值:

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

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

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

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
sql注入
反引号是个比较特别的字符,下面记录下怎么利用0x00SQL注入反引号可利用在分隔符及注释作用,不过使用范围只于表名、数据库名、字段名、起别名这些场景,下面具体说下1)表名payload:select\from\users\whereuser\_id1limit0,1;!(https://o
Wesley13 Wesley13
2年前
MySQL之SQL优化实战记录
MySQL之SQL优化实战记录背景本次SQL优化是针对javaweb中的表格查询做的。部分网络架构图!image(http://wx3.sinaimg.cn/mw690/006qiLqogy1fw41fuzn6uj30qg0gx3zo.jpg)业务简单说明N个机台将业务数据发送
Stella981 Stella981
2年前
Python3:sqlalchemy对mysql数据库操作,非sql语句
Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''
Stella981 Stella981
2年前
Prometheus监控学习笔记之PromQL简单示例
0x00简单的时间序列选择返回度量指标http_requests_total的所有时间序列样本数据:http_requests_total返回度量指标名称为http_requests_total,标签分别是job"apiserver",handler"/api/comments"
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
小万哥 小万哥
5个月前
MySQL 数据库表格创建、数据插入及获取插入的 ID:Python 教程
创建表格要在MySQL中创建表格,请使用"CREATETABLE"语句。确保在创建连接时定义了数据库的名称。示例创建一个名为"customers"的表格:pythonimportmysql.connectormydbmysql.connector.conn
小万哥 小万哥
5个月前
如何在 Python 中执行 MySQL 结果限制和分页查询
PythonMySQL限制结果限制结果数量示例1:获取您自己的Python服务器选择"customers"表中的前5条记录:Pythonimportmysql.connectormydbmysql.connector.connect(host"localh
小万哥 小万哥
5个月前
如何在 Python 中执行 MySQL 结果限制和分页查询
PythonMySQL限制结果限制结果数量示例1:获取您自己的Python服务器选择"customers"表中的前5条记录:pythonimportmysql.connectormydbmysql.connector.connect(host"localh
小万哥 小万哥
3个月前
深入了解 Python MongoDB 查询:find 和 find_one 方法完全解析
在MongoDB中,我们使用find()和findone()方法来在集合中查找数据,就像在MySQL数据库中使用SELECT语句来在表中查找数据一样查找单个文档要从MongoDB的集合中选择数据,我们可以使用findone()方法。findone()方法返