如何在MySQL中更新列

2024-09-30 04:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用从网站检索到的一些信息更新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')

Tags: in名称forexecutebsnamesmysqlall
1条回答
网友
1楼 · 发布于 2024-09-30 04:28:35

你有两个问题

首先,您缺少要设置的列的名称。第二,你不应该把占位符放在引号里cur.execute()为您执行必要的引号,因此您最终会在值中使用文字引号

cur.execute('UPDATE HuntsPointYelp SET names = %s', names)

相关问题 更多 >

    热门问题