Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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插入mysql表_Python_Mysql_Csv - Fatal编程技术网

无法使用python插入mysql表

无法使用python插入mysql表,python,mysql,csv,Python,Mysql,Csv,我正在尝试使用python中的csv文件在sql表中插入行 共有五列(id(自动增量、主、int)、事件(int)、版本(int)、订阅(varchar)、Daystogo(varchar) 我的csv有4列(事件、版本、订阅、Daystogo)。我想将它们插入表中,并让id以自动增量的方式分配 mydb = mysql.connector.connect( user='user', password='password',

我正在尝试使用python中的csv文件在sql表中插入行

共有五列(id(自动增量、主、int)、事件(int)、版本(int)、订阅(varchar)、Daystogo(varchar)

我的csv有4列(事件、版本、订阅、Daystogo)。我想将它们插入表中,并让id以自动增量的方式分配

mydb = mysql.connector.connect(
        user='user', password='password',
                              host='host',
                              database='opm')
mycursor = mydb.cursor()


csv_data=csv.reader("mycsvfile.csv")
for row in csv_data:
    print(row)
    mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)", (int,int,str, str))
这就是我一直收到的错误

csv_data=csv.reader("mycsvfile.csv")
for row in csv_data:
    print(row)
    mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)", (int,int,str, str))


['m']
Traceback (most recent call last):

  File "<ipython-input-51-5b5e2c804099>", line 4, in <module>
    mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)", (int,int,str, str))

  File "E:\Data Science\pyWork\PyProjects\Program\lib\site-packages\mysql\connector\cursor.py", line 547, in execute
    psub = _ParamSubstitutor(self._process_params(params))

  File "E:\Data Science\pyWork\PyProjects\Program\lib\site-packages\mysql\connector\cursor.py", line 430, in _process_params
    "Failed processing format-parameters; %s" % err)

ProgrammingError: Failed processing format-parameters; Python 'type' cannot be converted to a MySQL type
错误是

ProgrammingError: 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 '%s,%s,%s,%s)' at line 1

请帮助我如何在mysql表中插入行

假设您的CSV有四列

mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)", row)

应该可以工作(第二个参数需要是一个列表或一个元组,其中包含要绑定到占位符的元素;因为您有四个占位符,所以您需要一个四元素列表或数据元组(不是数据类型!)。

参数
(int,int,str,str)
应该做什么?您期望
(int,int,str,str)
to do?应该用包含要插入的值的变量替换。指定数据类型。我是新代码,并在某处看到此代码。如果我删除(int,int,str,str)时出错,请更正。它显示“ProgrammingError:您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解第1行“%s,%s,%s)”附近使用的语法是否正确:仅对可执行web代码(HTML+JS+CSS)使用堆栈溢出代码段。请使用普通代码格式(按四个空格缩进,可以通过选择代码并在Mac上点击Ctrl-K或Command-K来完成)对于任何其他类型的代码。它说编程错误:SQL语句的参数不够。我甚至尝试了元组(行)。但同样的错误您事先做了
打印(行)
,确认它实际上有四个元素。
mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)", row)