Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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/68.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 将日期从csv插入SQL中的列_Python_Mysql_Datetime - Fatal编程技术网

Python 将日期从csv插入SQL中的列

Python 将日期从csv插入SQL中的列,python,mysql,datetime,Python,Mysql,Datetime,我有像这样的csv数据 RegisterDate StoreID ProductID ProductType Quantity 30.12.2013 1002 00000576 001 10 1.1.2014 1002 00008577 001 20 2.1.2014 1002 00002917 002 12 3.1.2014 1002 000075

我有像这样的csv数据

RegisterDate    StoreID ProductID   ProductType Quantity
30.12.2013      1002    00000576    001         10
1.1.2014        1002    00008577    001        20
2.1.2014        1002    00002917    002        12
3.1.2014        1002    00007542    003         1
4.1.2014        1002    00000964    002         1
我想创建一个SQL表,我正在使用Python填充该表。以下是我的代码:

import pymysql as MS
import pandas as pd

db = MS.connect(host="localhost",user="root",passwd="passwd",db="db")
cursor = db.cursor()
cursor.execute("create table IF NOT EXISTS sales (productID INT (25), registerDate DATE, storeID INT(5), productType INT (3), Quantity INT (100));")

df = pd.read_csv('C:/Users/data.csv', encoding='latin-1')
for i in range (len(df)):
    date = pd.to_datetime(df['RegisterDate'][i], format='%d.%m.%Y') # Converting the field value to datetime
    data = [int(df['ProductID'][i]), date, int(df['StoreID'][i]), int(df['ProductType'][i]), int(df['Quantity'][i]]
    cursor.execute('INSERT INTO sales(productID, registerDate, storeID, productType, Quantity )' 'VALUES("%s", "%s", "%s", "%s", "%s"  )', data)

db.commit()
cursor.close()
但我得到了以下错误:

    Traceback (most recent call last):
  File "C:/Users/PycharmProjects/testproject/database.py", line 17, in <module>
    cursor.execute('INSERT INTO sales(productID, registerDate, storeID, productType, Quantity )' 'VALUES("%s", "%s", "%s", "%s", "%s"  )', data)
  File "C:\Python\Python35\lib\site-packages\pymysql\cursors.py", line 164, in execute
    query = self.mogrify(query, args)
  File "C:\Python\Python35\lib\site-packages\pymysql\cursors.py", line 143, in mogrify
    query = query % self._escape_args(args, conn)
  File "C:\Python\Python35\lib\site-packages\pymysql\cursors.py", line 118, in _escape_args
    return tuple(conn.literal(arg) for arg in args)
  File "C:\Python\Python35\lib\site-packages\pymysql\cursors.py", line 118, in <genexpr>
    return tuple(conn.literal(arg) for arg in args)
  File "C:\Python\Python35\lib\site-packages\pymysql\connections.py", line 821, in literal
    return self.escape(obj, self.encoders)
  File "C:\Python\Python35\lib\site-packages\pymysql\connections.py", line 814, in escape
    return escape_item(obj, self.charset, mapping=mapping)
  File "C:\Python\Python35\lib\site-packages\pymysql\converters.py", line 27, in escape_item
    val = encoder(val, mapping)
  File "C:\Python\Python35\lib\site-packages\pymysql\converters.py", line 110, in escape_unicode
    return u"'%s'" % _escape_unicode(value)
  File "C:\Python\Python35\lib\site-packages\pymysql\converters.py", line 73, in _escape_unicode
    return value.translate(_escape_table)
AttributeError: 'Timestamp' object has no attribute 'translate'
回溯(最近一次呼叫最后一次):
文件“C:/Users/PycharmProjects/testproject/database.py”,第17行,在
cursor.execute('INSERT INTO sales(productID、registerDate、storeID、productType、Quantity)'值(“%s”、“s”、“s”、“s”)、数据)
文件“C:\Python\Python35\lib\site packages\pymysql\cursors.py”,第164行,在execute中
query=self.mogrify(query,args)
文件“C:\Python\Python35\lib\site packages\pymysql\cursors.py”,第143行,在mogrify中
查询=查询%self.\u转义\u参数(参数,conn)
文件“C:\Python\Python35\lib\site packages\pymysql\cursors.py”,第118行,在参数中
返回元组(args中arg的conn.literal(arg))
文件“C:\Python\Python35\lib\site packages\pymysql\cursors.py”,第118行,在
返回元组(args中arg的conn.literal(arg))
文件“C:\Python\Python35\lib\site packages\pymysql\connections.py”,第821行,文本
返回自转义(obj,自编码器)
文件“C:\Python\Python35\lib\site packages\pymysql\connections.py”,第814行,位于escape中
返回转义项(obj,self.charset,mapping=mapping)
文件“C:\Python\Python35\lib\site packages\pymysql\converters.py”,第27行,在escape\u项中
val=编码器(val,映射)
文件“C:\Python\Python35\lib\site packages\pymysql\converters.py”,第110行,以escape\u unicode格式
返回u“'%s'%”\u转义\u unicode(值)
文件“C:\Python\Python35\lib\site packages\pymysql\converters.py”,第73行,采用unicode格式
返回值.translate(_escape_表)
AttributeError:“Timestamp”对象没有属性“translate”

我搜索了上述错误,但没有找到相关的答案。如何用其他列填充日期?

我将使用
strftime(“%Y-%m-%d”)方法将您的
datetime
对象转换为python中特定格式的字符串插入MySQL中:

data = [int(df['ProductID'][i]), date.strftime('%Y-%m-%d'), int(df['StoreID'][i]), int(df['ProductType'][i]), int(df['Quantity'][i]]

通过这种方式,您传递的字符串格式为
“%Y-%m-%d”
,它符合MySQL的日期文本标准定义。

首先确定您使用的是哪种RDBMS。顺便说一句,我使用的是MySQL,
int
关键字后面括号中的那些小数字实际上没有任何意义。我尝试过,但现在出现了以下错误
pymysql.err.InternalError:(第1行“registerDate”列的1292,“不正确的日期值:“”30-12-2013”)
那么您一定使用了与我建议的格式不同的字符串。我已选中了要插入SQL的数据列表
,该列表具有字符串日期,但为什么?我已在这里转换为日期对象。因此,它将对象转换为特定格式
%Y-%m-%d
,但当我在列表中打印它时,它是一个字符串