我试图在本地数据库中插入一些数据(存储在元组列表中)。数据有点不一致-有些时间戳是datetime.datetime
,而有些时间戳可以是字符串。但我不认为这是我的问题所在。在
首先,我的数据库模式:
+-----------------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------+-------------+------+-----+---------+-------+
| store | varchar(11) | NO | | NULL | |
| order_no | int(11) | NO | | NULL | |
| product_name | text | YES | | NULL | |
| product_id | int(11) | NO | | NULL | |
| classification | int(11) | NO | | NULL | |
| order_date | datetime | NO | | NULL | |
| power_complete_time | datetime | YES | | NULL | |
| stockout_date | datetime | YES | | NULL | |
| wfi_status | tinyint(4) | YES | | NULL | |
| qc_status | varchar(12) | YES | | NULL | |
| scanned_for_shipment_date | datetime | YES | | NULL | |
| order_delivered_date | datetime | YES | | NULL | |
| status | varchar(50) | YES | | NULL | |
| stock_out_status | varchar(50) | YES | | NULL | |
| actual_order_date | datetime | NO | | NULL | |
| order_for_today | tinyint(1) | YES | | NULL | |
| dispatched_within_same_day | tinyint(1) | YES | | NULL | |
| stockout_within_same_day | tinyint(1) | YES | | NULL | |
| order_for_yesterday | tinyint(1) | YES | | NULL | |
| dispatched_within_yesterday | tinyint(1) | YES | | NULL | |
| stockout_within_yesterday | tinyint(1) | YES | | NULL | |
| eyeframe_less_than_1_pm | tinyint(1) | YES | | NULL | |
+-----------------------------+-------------+------+-----+---------+-------+
接下来是我的脚本,里面有疯狂的奇怪数据。同样,一些日期的数据可能不一致,这就是为什么我要用一堆数据行进行测试:
^{pr2}$我得到的错误是:
Traceback (most recent call last):
File "C:\Users\Karan\Desktop\Dropbox\Valyoo\Task 3\daily_dispatch phpmyadmin\local\insert.py", line 50, in <module>
row[18], row[19], row[20], row[21]))
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 'Card, 41420, shipping_accessories, 2013-07-27 04:12:56, 2013-07-27 11:18:04,\n ' at line 1")
我不知道怎么了。有人帮我调试这个吗?
有没有一种更好的(视觉上和性能上的)插入元组的方法,而不是使用索引?
您正在插值数据,将其留给数据库适配器,后者将根据需要处理各种值的转义。还有否需要展开整个行值列表:
实际上,您不必键入所有这些
^{pr2}$%s
占位符;让Python为您生成它们:由于您有多行,请让数据库循环遍历这些行,然后依次对每一行执行相同的查询:
你不需要绕圈子。在
你需要正确地引用论点。但是,您不必手动执行此操作。相反,将参数作为第二个参数传递给
cursor.execute
,MySQLdb将为您引用它们:只插入第一行:
而不是
cur.execute(sql % row[0])
。这看起来是一个微妙的变化,但它使世界变得不同。在要插入所有行,请使用:
^{pr2}$相关问题 更多 >
编程相关推荐