gpt4 book ai didi

python - 不知道这个 : "pymysql.err.ProgrammingError: (1064..." 发生了什么

转载 作者:太空宇宙 更新时间:2023-11-03 11:43:56 28 4
gpt4 key购买 nike

这是我的代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
import re
import pymysql

resp = urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8")

soup = bs(resp ,"html.parser")

listUrls = soup.findAll("a", href=re.compile("^/wiki/"))

for url in listUrls:

if not re.search('\.(jpg|JPG)$', url['href']):

conn = pymysql.connect(
host='127.0.0.1',
user='root',
password='',
db='wikiurl',
charset='utf8mb4'
)
try:
with conn.cursor() as cursor:

sql = "insert into 'wikiurl'('urlname','urlhref') VALUES (%s , %s)"
cursor.execute(sql,(url.get_text(), "https://en.wikipedia.org" + url["href"]))
conn.commit()

finally:
conn.close()

错误:

pymysql.err.ProgrammingError: (1064, "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 ''wikiurl'('urlname','urlhref') VALUES ('Wikipedia' , 'https://en.wikipedia.org/w' at line 1")

最佳答案

首先,我建议对细节给予最大的关注。

试试这个:

sql = "INSERT INTO wikiurl (urlname, urlhref) VALUES (%s, %s)"

另请注意,表名周围不需要单引号。请参阅:MySQL Insert documentation.

编辑:您不需要在列名两边加上引号。

关于python - 不知道这个 : "pymysql.err.ProgrammingError: (1064..." 发生了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39981248/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com