[英]Error with Mysql Syntax in Python Script when Inserting into Database
I'm very new to python and mysql also and am currently trying to "INSERT data into a table, events for which I have set up on a WAMP server on my machine. The data I need to store into the database are stored within various arrays within my python program and these have all been stored correctly. I am able to successfully connect to the localhost database via my python program however I am having issues with my mysql syntax when writing to the database. 我也是python和mysql的新手,目前正在尝试“将数据插入表中,这些事件已在我的计算机上的WAMP服务器上设置。我需要存储到数据库中的数据存储在各种数据库中我的python程序中的数组,这些数组都已正确存储。我能够通过python程序成功连接到localhost数据库,但是在写入数据库时,我的mysql语法存在问题。
I hope someone is able to help me - I've basically set up a for loop to write elements stored in an array to the database that I have set up locally on my machine (localhost). 我希望有人能够帮助我-我基本上已经建立了一个for循环,将存储在数组中的元素写入到我在本地计算机(本地主机)上设置的数据库中。
length = range(len(event_ids))
imageid = 0
for z in length:
cur.execute("""INSERT INTO events (event_id, event_title, start_time, stop_time, venue_id, image_id, all_day, venue_name) VALUES (%s, %s, %s, %s, %s, %s, %s, %s""" %(event_ids[z], event_names1[z], start_times1[z], stop_times1[z], venue_ids1[z], imageid, all_day_list1[z], venue_names1[z]))
db.commit()
cursor.close()
db.close()
The errors that I receive includes the below information. 我收到的错误包括以下信息。 The elements that are displayed here are reading from the arrays that I have specified successfully "@2013042020, Prodijig Dublin, 2013-04-20 20:00:00, 2013-04-20 23:59:00, V0-001-0:".
此处显示的元素是从我已成功指定的数组中读取的:“ @ 2013042020,Prodijig Dublin,2013-04-20 20:00:00,2013-04-20 23:59:00,V0-001-0 :“。 I just do not know why I receive SQL syntax errors.
我只是不知道为什么我会收到SQL语法错误。 I'd really appreciate it if anyone can shed any information on where I may be going wrong.
如果有人能提供有关我可能会出错的地方的任何信息,我将非常感激。
ERRORS: 错误:
cur.execute("""INSERT INTO events (event_id, event_title, start_time, stop_time, venue_id, image_id, all_day, venue_name) VALUES (%s, %s, %s, %s, %s, %s, %s, %s""" %(event_ids[z], event_names1[z], start_times1[z], stop_times1[z], venue_ids1[z], imageid, all_day_list1[z], venue_names1[z]))
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 202, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
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 '@2013042020, Prodijig Dublin, 2013-04-20 20:00:00, 2013-04-20 23:59:00, V0-001-0' at line 1")
EDIT: I have a fix for this code after below discussions however I am now receiving an "IndexError: list index out of range" error for some reason I cannot explain at present and my python program terminates after 35 iterations (when it should iterate and insert elements to database 50 times). 编辑:在下面的讨论之后,我对此代码进行了修复,但是由于某些原因,我现在收到“ IndexError:列表索引超出范围”错误,由于目前无法解释,并且我的python程序在35次迭代后终止(应进行迭代和将元素插入数据库50次)。 If anyone could help me with this too I'd be very appreciative.
如果有人能帮助我,我将非常感激。 It might not have to do with this statement however - I am unsure just now.
但是,这可能与该语句无关-我现在不确定。
My fixed mysql insert into code anyway is: 我固定的mysql插入代码无论如何是:
cur.execute('INSERT INTO events (event_id, event_title, start_time, stop_time, venue_id, image_id, all_day, venue_name) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)' , (event_ids[z], event_names1[z], start_times1[z], stop_times1[z], venue_ids1[z], imageid, all_day_list1[z], venue_names1[z]))
您需要在所有插入的字符串值周围加上引号:
cur.execute("""INSERT INTO events (event_id, event_title, start_time, stop_time, venue_id, image_id, all_day, venue_name) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'""" %(event_ids[z], event_names1[z], start_times1[z], stop_times1[z], venue_ids1[z], imageid, all_day_list1[z], venue_names1[z]))
The query you are running is this: 您正在运行的查询是这样的:
INSERT INTO events (
event_id,
event_title,
start_time,
stop_time,
venue_id,
image_id,
all_day,
venue_name)
VALUES (
@2013042020,
Prodijig Dublin,
2013-04-20 20:00:00,
2013-04-20 23:59:00,
V0-001-0 (rest of query is missing)
MySQL is complaining that @2013042020
is not a valid value. MySQL抱怨
@2013042020
不是有效值。 I assume event_id
is an integer column, so the value should not contain an @
symbol. 我假设
event_id
是一个整数列,所以该值不应包含@
符号。 Once you fix that you will notice that MySQL has more to complain about your query: 修复后,您会发现MySQL对您的查询还有更多抱怨:
Prodijig Dublin should be a string: 'Prodijig Dublin'
2013-04-20 20:00:00 should be a date : #2013-04-20 20:00:00# (quotes also work)
2013-04-20 23:59:00 should be a date : #2013-04-20 23:59:00#
V0-001-0 should be as tring: 'V-0-001-0'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.