python - ProgrammingError:“[”附近的SQL语法是什么意思?
问题描述
有人可以解释一下这是什么意思吗?
ProgrammingError: You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near '[' at line 1
是不是在我的代码中“[”附近的某个地方我做错了什么?
此代码块中发生错误。我不明白为什么它说它在“[”附近,因为那段代码事先运行良好?谢谢
from bs4 import BeautifulSoup
from urllib.request import urlopen as uReq
p_list = []
n_list = []
h_list = []
ba_list = []
be_list = []
all_var =[]
for page in range(20,300,20):
my_url = "https://www.daft.ie/ireland/property-for-sale/?
offset=20".format(page)
#open connection and grab webpage
uClient = uReq(my_url)
#store html in a variable
page_html = uClient.read()
#close web connection
uClient.close()
#parse html
soup = BeautifulSoup(page_html, "html.parser")
print(soup)
#grabs listings house information
listings = soup.findAll("div",
{"class":"FeaturedCardPropertyInformation__detailsContainer"})
for container in listings:
#extracting price
price = container.div.div.strong.text
price = price.strip('AMV: €')
price = price.strip('Reserve: €')
price = price.replace(',', "")
price = int(price)
p_list.append(price)
#location
location = container.div.find("a {"class":"PropertyInformationCommonStyles__addressCopy--link"}).text
n_list.append("'"+location+"'")
#house type
house = container.div.find("div",
{"class":"QuickPropertyDetails__propertyType"}).text
h_list.append("'"+house+"'")
#number of bathrooms
bath_num = container.div.find("div {"class":"QuickPropertyDetails__iconCopy--WithBorder"}).text
#makes str and int
bath_num = int(bath_num)
ba_list.append(bath_num)
#number of bedrooms
bed_num = container.div.find("div", {"class":"QuickPropertyDetails__iconCopy"}).text
bed_num = int(bed_num)
be_list.append(bed_num)
#makes str and int
all_var.append((price, location, house, bath_num, bed_num))
a_v = str(all_var)
#connecting to database
import mysql.connector
d_b = mysql.connector.connect(host = "localhost", user = "myaccount", passwd = "mypassword", database = "database",)
print(d_b)
mycursor = d_b.cursor(buffered=True)
#create database
#mycursor.execute("CREATE DATABASE daftdatabase")
#create table
#mycursor.execute("CREATE TABLE DaftTable(price Integer(10), location
VARCHAR(50), type VARCHAR(20), bedrooms INTEGER(2), bathrooms
INTEGER(2))")
show_t = mycursor.execute("SHOW TABLES")
for var in a_v:
data = mycursor.execute("INSERT INTO DaftTable(price, location, type, bathrooms, bedrooms) VALUES"+var)
mycursor.commit()
每次我确实修复一个错误时,另一个错误不断发生,我只想了解错误告诉我什么以及如何修复它?
解决方案
该错误告诉您您正在以错误的方式编写 SQL。
首先,您应该逃避/清理您插入的任何内容。如果你像这样组成字符串,充其量它很有可能在某些时候会失败(例如,因为未转义的单引号),最坏的情况是如果你在代码的其他部分这样做,你会有一个 SQL 注入漏洞
其次,即使你逃脱了一切,它仍然行不通。
您正在获取值,将它们放入列表中,然后将其字符串化:
all_var.append((price, location, house, bath_num, bed_num))
a_v = str(all_var)
这意味着如果你有类似的东西
[
(1, 2, 3, 4),
(5, 6, 7, 8),
]
对于您的值,您将其转换为字符串[(1, 2, 3, 4), (5, 6, 7, 8)]
,并且您的for
循环将遍历该字符串的单个字符。
第一个字符是[
给你错误的,因为你正在尝试执行
INSERT INTO DaftTable(price, location, type, bathrooms, bedrooms) VALUES[")
因此,您需要的是不对值进行字符串化并以正确的方式组合字符串(如果您真的想组合字符串而不是使用某种类型的 SQL 库,至少使用%s
语法)。
推荐阅读
- vba - 使用 vba 和命名范围创建条件列表
- c# - 正在序列化的类中不存在的类的序列化错误
- c# - 如何从动态数组中获取值?
- c# - 我无法在我指定的文件夹中保存从 pictureBox1 拍摄的照片
- android - Expo本地身份验证不适用于非生物识别设备
- javascript - 如何更新状态以触发异步承诺回调中的渲染更改?
- xamarin - Xamarin Forms Prism 应用程序使用 DryIoc 容器。如何注册“IBleManager”?
- html - 使用 p 标签的嵌套 v-for 循环在 Vue.js 中不起作用
- python-3.x - Python 数据保存性能
- swift - 如何在 Swift 中创建一个表格视图,其中每个单元格都推送到新的表格视图