我想用从网站检索到的一些信息更新 MySQL 表中的列。该列的名称是“names”,表的名称是“HuntsPointYelp”
我的更新查询出现编程错误,我认为这是一个语法错误。
谢谢!
import pymysql
conn = pymysql.connect(host='127.0.0.1',
unix_socket='/tmp/mysql.sock',user='root',
passwd=' ', db='mysql', charset='utf8')
cur = conn.cursor()
cur.execute('USE HuntsPointsBusinesses')
def storeNames (names):
cur.execute('UPDATE HuntsPointYelp SET = ("%s")', names)
cur.connection.commit()
def getNames (bs):
#names:
restGrid = bs.find_all ("ul", {"class": "lemon--ul__373c0__1_cxs
undefined list__373c0__2G8oH"})
#namesList = []
time.sleep(2)
for i in restGrid:
h3 = i.find_all ("h3")
for h in h3:
target = h.find_all ("a")
for t in target:
if "name" in t.attrs:
if t.attrs is not None:
names = t["name"]
storeNames (names)
driver.get ("https://www.yelp.com/search?
cflt=restaurants&find_loc=Hunts+Point%2C+Bronx%2C+NY+10474")
pageSource = driver.page_source
bs = BeautifulSoup (pageSource, "html.parser")
names = getNames(bs)
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 \'= ("\'Roaming Woodfired Pizza\'")\'
at line 1')
最佳答案
你有两个问题。
首先,您缺少要设置的列的名称。其次,不应将占位符放在引号中; cur.execute()
为您执行必要的引用,因此您最终会在值中得到文字引号。
cur.execute('UPDATE HuntsPointYelp SET names = %s', names)
关于python - 如何更新 MySQL 中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57334506/