0

我尝试使用 python 将数据插入表中,但出现语法错误。这是我的表格格式。

mycursor.execute("""CREATE TABLE workers (
                 `ID` int(5) NOT NULL AUTO_INCREMENT,
                 ` x` FLOAT NOT NULL,
                 ` y` FLOAT NOT NULL,
                 `z` FLOAT NOT NULL,
                 `Acc` int NOT NULL,
                 `Datasource` char(3) NOT NULL,
                 `Datatype` int(5) NOT NULL,
         PRIMARY KEY (`ID`))""")

表格代码工作正常

这是我试图插入这些表的数据

mycursor.execute("""INSERT INTO workers VALUES
(1,\ 
1.2720693 ,\
6.583606 ,\
6.8299093 ,\
3 ,\
"Acc" ,\
1)""")

这是我得到的错误

mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '\ 
1.2720693 ,6.583606 ,6.8299093 ,3 ,"Acc" ,1)' at line 2
4

1 回答 1

1

您从服务器返回的错误告诉您反斜杠是问题所在。

mycursor.execute("""
                 INSERT INTO workers VALUE (1,                    
                 1.2720693,
                 6.583606,
                 6.8299093,
                 3,
                 "Acc",
                 1)
                 """)

python 中的三重字符串是为多行字符串设计的,因此您不需要使用反斜杠处理行继续。您的INSERT语句中还有 7 个字段值,但您只需要 6 个,因为ID列是自动递增的。

于 2015-11-27T23:34:41.347 回答