Electro01 Electro01 - 1 month ago 11
MySQL Question

Don't know what happened with this : "pymysql.err.ProgrammingError: (1064..."

Here is my code:

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()


Error:


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")

Answer

First of all, I recommend giving whitespace the utmost attention to detail.

Try this:

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

Also notice that single quotation marks are not necessary around the table name. See: MySQL Insert documentation.

Edit: And you don't need quotation marks around the column names.