python - 如何更新 MySQL 中的列

标签 python mysql

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

相关文章:

php - 使用 PHP 从数据库中获取选定的下拉选项

php - "cannot connect using the old insecure authentication"但我没有旧密码?

mysql - 在 Mysql 严格模式下进行算术运算时防止超出范围的错误

python - 具有亚秒级(例如毫秒)分辨率的 to_datetime

python - 跳过损坏的 jsons python

python - Matplotlib "pick_event"无法在带有 FigureCanvasTkAgg 的嵌入式图形中工作

python - python有数量限制吗?

python - 监控数据库相关事件的最佳实践

mysql - 迭代列并总结结果

mysql - 具有多个连接的计数