首页 > 解决方案 > 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() 

每次我确实修复一个错误时,另一个错误不断发生,我只想了解错误告诉我什么以及如何修复它?

标签: pythonmysql

解决方案


该错误告诉您您正在以错误的方式编写 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语法)。


推荐阅读