繁体   English   中英

插入数据库时​​,Python脚本中的Mysql语法错误

[英]Error with Mysql Syntax in Python Script when Inserting into Database

我也是python和mysql的新手,目前正在尝试“将数据插入表中,这些事件已在我的计算机上的WAMP服务器上设置。我需要存储到数据库中的数据存储在各种数据库中我的python程序中的数组,这些数组都已正确存储。我能够通过python程序成功连接到localhost数据库,但是在写入数据库时​​,我的mysql语法存在问题。

我希望有人能够帮助我-我基本上已经建立了一个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()

我收到的错误包括以下信息。 此处显示的元素是从我已成功指定的数组中读取的:“ @ 2013042020,Prodijig Dublin,2013-04-20 20:00:00,2013-04-20 23:59:00,V0-001-0 :“。 我只是不知道为什么我会收到SQL语法错误。 如果有人能提供有关我可能会出错的地方的任何信息,我将非常感激。

错误:

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") 

编辑:在下面的讨论之后,我对此代码进行了修复,但是由于某些原因,我现在收到“ IndexError:列表索引超出范围”错误,由于目前无法解释,并且我的python程序在35次迭代后终止(应进行迭代和将元素插入数据库50次)。 如果有人能帮助我,我将非常感激。 但是,这可能与该语句无关-我现在不确定。

我固定的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]))

您正在运行的查询是这样的:

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抱怨@2013042020不是有效值。 我假设event_id是一个整数列,所以该值不应包含@符号。 修复后,您会发现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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM