Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 操作错误:接近“;系统";:语法错误 OperationalError回溯(最近一次调用) 在() 1表示df.ItErrors()中的行: 2 sql='插入salesdata({})值({})'.format(','.join(df.columns),','.join(['?']*len(df.columns))) ---->3 c.execute(sql,元组(行[1])) 4康涅狄格州提交_Python_Python 3.x - Fatal编程技术网

Python 操作错误:接近“;系统";:语法错误 OperationalError回溯(最近一次调用) 在() 1表示df.ItErrors()中的行: 2 sql='插入salesdata({})值({})'.format(','.join(df.columns),','.join(['?']*len(df.columns))) ---->3 c.execute(sql,元组(行[1])) 4康涅狄格州提交

Python 操作错误:接近“;系统";:语法错误 OperationalError回溯(最近一次调用) 在() 1表示df.ItErrors()中的行: 2 sql='插入salesdata({})值({})'.format(','.join(df.columns),','.join(['?']*len(df.columns))) ---->3 c.execute(sql,元组(行[1])) 4康涅狄格州提交,python,python-3.x,Python,Python 3.x,我得到了操作错误:接近“系统”:语法错误。我试着在单引号之间插入更多的空格并删除空格,但这不起作用。我如何使这个代码工作 您的一个列名大概是System。这是MySQL 8.0中的保留字,因此需要对其进行转义。您应该在所有列名周围加上反勾号 OperationalError Traceback (most recent call last) <ipython-input-7-e63de1bbf3b5> in <module>

我得到了操作错误:接近“系统”:语法错误。我试着在单引号之间插入更多的空格并删除空格,但这不起作用。我如何使这个代码工作

您的一个列名大概是
System
。这是MySQL 8.0中的保留字,因此需要对其进行转义。您应该在所有列名周围加上反勾号

OperationalError                          Traceback (most recent call last)
<ipython-input-7-e63de1bbf3b5> in <module>()

      1 for row in df.iterrows():
      2        sql = 'INSERT INTO salesdata ({}) VALUES ({})'.format(','.join(df.columns), ','.join(['?']*len(df.columns)))
----> 3        c.execute(sql, tuple(row[1]))
      4 conn.commit()

我建议记录
sql
字符串的文本内容和作为参数传递的元组的文本内容。现在这个问题中没有足够的内容。(我假设
System
是您的数据帧的列名之一,但最好有一个独立的,不需要假设)。
System
是MySQL 8.0中的保留字。您需要将其放在反勾号中。您应该使用插入参数,而不是字符串格式。@Keith,……但在本例中,它们使用字符串格式插入列名和占位符,以便DBAPI使用。看到里面的
.format()
,我也会有类似的下意识反应,这是他们问题的根源,因为他们做的比SQLAlchemy更糟糕。。。但是OP并不是来自一个完全不好的地方。
sql = 'INSERT INTO salesdata ({}) VALUES ({})'.format(','.join('`' + col + '`' for col in df.columns), ','.join(['?']*len(df.columns)))