gpt4 book ai didi

mysql - Python3 SQL 插入问题

转载 作者:行者123 更新时间:2023-11-29 09:34:46 25 4
gpt4 key购买 nike

我遇到了以下问题。它说

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 'INSERT\xc2\xa0INTO\xc2\xa0no.top_new VALUES\xc2\xa0(title,\xc2\xa0link,\xc2\xa0description)' at line 1")

我不确定查询出了什么问题?

 import feedparser, MySQLdb
db = MySQLdb.connect(host='localhost',user='root',passwd='sdsdf', db='no')
mycursor = db.cursor()
d = feedparser.parse('https://news.yahoo.com/rss/entertainment')

for post in d.entries:
title = post.title
link = post.link
description = post.description
sql = '''INSERT INTO no.top_new VALUES (title, link, description)'''
mycursor.execute(sql)

最佳答案

您没有在 INSERT 查询中指定列。这可以。但是,您必须确保表的列数与插入的值完全相同(在本例中为三),并且表结构中的列的顺序与 INSERT 值的顺序相同。因此,我怀疑您的表要么有额外的列,要么没有按照给定值的顺序构建:tite|link|description。

编辑

结构是否正确。那么问题可能与插入的数据有关,其中可能包含无效字符。

解决方案:按如下方式重写 INSERT 查询:

sql = "INSERT INTO no.top_new VALUES (%s, %s, %s)"
mycursor .execute(sql, (title, link,description))

这样,要插入的字符串就会被转义。

关于mysql - Python3 SQL 插入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57877904/

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