我有几个查询。我的大多数插入查询都有效,并且它们遵循与该插入查询相同的格式(我什至可以复制粘贴并根据需要进行修改)。由于某种原因,此查询使我陷入语法错误,但我不确定为什么。
我已经查看了SO上用于解决此错误的各种解决方案。似乎人们是出于各种不同的原因而获得它的,而我不确定到底是什么原因导致了此错误。我看不到该语句有问题。
def set_prices(game_name, vendor_id, vendor_name, game_id, game_platform, vendor_store_link, current_price_at_vendor, previous_price_at_vendor, historical_low_at_vendor):
# Create the query
insert_price_data = """ INSERT INTO game_vendors
(game_name, vendor_id, vendor_name, game_id, game_platform, vendor_store_link, current_price_at_vendor, previous_price_at_vendor, historical_low_at_vendor, historical_low_date)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s); """
vals = (game_name, vendor_id, vendor_name, game_id, game_platform, vendor_store_link, current_price_at_vendor, previous_price_at_vendor, historical_low_at_vendor, datetime.datetime.now().strftime("%Y/%m/%d").replace('/', '-'))
send_to_db(insert_price_data, vals)
set_prices("borderlands 3", "1", "xbox", "26", "xbox one", "xbox.com", "45", "45", "25")
我没想到会收到此错误,因为以前的任何SQL语句都没有发生此错误。
“ 1064(42000):您的SQL语法有错误;请查看手册 对应于您的MySQL服务器版本的正确语法, 在第4行的'%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'附近使用
我敢打赌,这是我忽略的小事。
更新send_to_db()
函数:
def send_to_db(query, multi_result=False, vals=None):
# this function opens a database query for a connection
# build engine for database
db_engine = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="mydb",
)
# Initiate the cursor
cursor = db_engine.cursor()
# row = None
# rows = None
try:
if vals:
# insert queries require vals
cursor.execute(query, vals)
db_engine.commit()
else:
# select queries which don't require vals
cursor.execute(query)
if multi_result:
rows = cursor.fetchall()
return rows
else:
row = cursor.fetchone()
return row
except mysql.connector.Error as error:
print(error)
finally:
if db_engine.is_connected():
cursor.close()
db_engine.close()
print("MySQL connection is closed")
db_engine.disconnect()