Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我使用python将datetime从csv文件插入mysql时,我遇到了一个错误_Python_Mysql - Fatal编程技术网

当我使用python将datetime从csv文件插入mysql时,我遇到了一个错误

当我使用python将datetime从csv文件插入mysql时,我遇到了一个错误,python,mysql,Python,Mysql,我将datetime从csv插入mysql,我得到以下错误 _mysql_exceptions.OperationalError: (1292, "Incorrect datetime value: '8/13/2017 19:10' for column 'js_signup' at row 1"). 我的约会时间是这样的“8/10/2017 12:59” 我的代码如下 import csv import MySQLdb mydb = MySQLdb.connect(host='localh

我将datetime从csv插入mysql,我得到以下错误

_mysql_exceptions.OperationalError: (1292, "Incorrect datetime value: '8/13/2017 19:10' for column 'js_signup' at row 1").
我的约会时间是这样的“8/10/2017 12:59”

我的代码如下

import csv
import MySQLdb
mydb = MySQLdb.connect(host='localhost',user='root',passwd='Yogi123$',db='wisdom_india')
 cursor = mydb.cursor()
csv_data = csv.reader(file('test_csv.csv'))
for row in csv_data:
    cursor.execute("""INSERT INTO testcsv(js_id,
    js_name,js_email,js_signup)
    VALUES(%s,%s,%s,STR_TO_DATE(%s,'%%%m/%%%d/%%%Y %%%h:%%%i'))""", row)
mydb.commit()
cursor.close()
print "Done"

您可以在本机python代码中重新格式化日期,而不是使用SQL吗?请参见下面的示例(注意,这仅是指示性的)

如果您想让我编写代码并测试它,或者有任何其他问题,请告诉我

以下是一些更新的代码(为了方便起见对您的代码进行了一些更改),用于上载此数据:

1,2,3,01-13-2017 13:00
1,2,3,01-13-2017 13:00
1,2,3,01-13-2017 13:00
在此表中:

CREATE TABLE testLoad(js_id INT UNSIGNED, js_name INT UNSIGNED, js_email INT UNSIGNED, js_signup DATETIME);
像这样:

import csv
import MySQLdb
import datetime
mydb = MySQLdb.connect(host='localhost',user='root',passwd='',db='test')
cursor = mydb.cursor()
with open('test_csv.csv', 'r') as csvfile:
    csv_data = csv.reader(csvfile)
    for row in csv_data:
        VInsert = "INSERT INTO testLoad(js_id, js_name,js_email,js_signup) VALUES(" + row[0] + ", " + row[1] + ", " + row[2] + ", '" + datetime.datetime.strptime(row[3], '%m-%d-%Y %H:%M').strftime('%Y-%m-%d %H:%M') + "')"
        cursor.execute(VInsert)
mydb.commit()
cursor.close()
print "Done"
根据这些结果:

SELECT * FROM testLoad;
+-------+---------+----------+---------------------+
| js_id | js_name | js_email | js_signup           |
+-------+---------+----------+---------------------+
|     1 |       2 |        3 | 2017-01-13 13:00:00 |
|     1 |       2 |        3 | 2017-01-13 13:00:00 |
|     1 |       2 |        3 | 2017-01-13 13:00:00 |
+-------+---------+----------+---------------------+
3 rows in set (0.00 sec)
问候,


James

我在插入日期时间时出错了。请有人帮我解决这个问题。对于您的数据库,“2017年8月13日”是13个月的第8天。确实不可能。当我使用STR_来确定它的给定错误“TypeError:没有足够的参数用于格式字符串”时,我尝试用你的答案回答它的给定错误“TypeError:没有足够的参数用于格式字符串”好的,在我的答案中添加了一点,现在已经尝试并测试了
SELECT * FROM testLoad;
+-------+---------+----------+---------------------+
| js_id | js_name | js_email | js_signup           |
+-------+---------+----------+---------------------+
|     1 |       2 |        3 | 2017-01-13 13:00:00 |
|     1 |       2 |        3 | 2017-01-13 13:00:00 |
|     1 |       2 |        3 | 2017-01-13 13:00:00 |
+-------+---------+----------+---------------------+
3 rows in set (0.00 sec)