【发布时间】:2019-03-20 12:04:30
【问题描述】:
我正在尝试使用 python 中的 csv 文件在我的 sql 表中插入行。
一共有五列(id(autoincrement, primary, int),Event(int), Edition(int), Subscription(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
代码
mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)")
错误是
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 表中插入行
【问题讨论】:
-
(int,int,str, str)应该做什么参数? -
您希望
(int,int,str, str)做什么?这应该替换为包含您要插入的值的变量。 -
指定数据类型。我是新手,在某处看到此代码。如有错误请指正
-
当我删除(int,int,str,str)时。它说“ProgrammingError:您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '%s,%s,%s,%s)' 附近使用正确的语法”
-
未来:仅将 Stack Overflow Snippets 用于可执行的 Web 代码 (HTML+JS+CSS)。对任何其他类型的代码使用正常的代码格式(缩进四个空格,可以通过选择代码并按 Ctrl-K 或 Mac 上的 Command-K 来完成)。