python - MysqlPython : Not all parameters were used in MySQL statement

标签 python mysql python-2.7

所以我试图将数据作为变量输入到我的表中,但我似乎不断收到错误 mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statements,我使用了 %s 而不是行名称,但我似乎不断收到相同的错误。我怀疑这是我的语法,但我似乎无法弄清楚这是我第一次同时使用 python 和 MySQL

import mysql.connector


Sensor_ID = "test"

Location = "room"

Sensor_IP = "192.168.1.1"

Sensor_1 = "10"

Sensor_1_Unit = "*C"

Sensor_2 =""

Sensor_2_Unit = ""

Sensor_3 = ""

Sensor_3_Unit = ""

conn = mysql.connector.connect(user='******', password='********',    host='******', database='*****') #blanked my user n pass
mycursor = conn.cursor()
mycursor.execute('SHOW TABLES')
print(mycursor.fetchall())
print ""

mycursor.execute("SHOW VARIABLES LIKE '%version%'")
print "Version:",(mycursor.fetchall())
#works up to here

mycursor.execute("INSERT INTO iot_sensors VALUES (ID, Sensor_ID, Location, Sensor_IP, Sensor_1, Sensor_1_Unit, Sensor_2,Sensor_2_Unit, Sensor_3,   Sensor_3_Unit)",(Sensor_ID,Location,Sensor_IP,Sensor_1,Sensor_1_Unit,Sensor_2,Sensor_2_Unit,Sens or_3,Sensor_3_Unit))
conn.commit()

  #    Sensor_ID,Location,Sensor_IP,Sensor_1,Sensor_1_Unit,Sensor_2,Sensor_2_Unit,Sensor_3,Sensor_3_Unit

创建表 IoT_Sensors(

ID INT NOT NULL AUTO_INCRMENT,

Sensor_ID VARCHAR (15) NOT NULL,

位置 VARCHAR (20) NOT NULL,

Sensor_IP VARCHAR (15) NOT NULL,

Sensor_1 VARCHAR (15) NOT NULL,

Sensor_1_Unit VARCHAR (15) NOT NULL,

Sensor_2 VARCHAR (15),

Sensor_2_Unit VARCHAR (15),

Sensor_3 VARCHAR (15),

Sensor_3_Unit VARCHAR (15),

Time_Stamp 时间戳不为空,

主键(ID));

最佳答案

您需要将 %s 放入 SQL 语句中。

mycursor.execute("INSERT INTO iot_sensors VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",(Sensor_ID,Location,Sensor_IP,Sensor_1,Sensor_1_Unit,Sensor_2,Sensor_2_Unit,Sens or_3,Sensor_3_Unit))

请参阅 docs 中的示例.

关于python - MysqlPython : Not all parameters were used in MySQL statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42617449/

相关文章:

python - 从 GStreamer 实时接收 Numpy 数组

mysql - 根据mysql中的两列选择唯一行

MySQL LEFT JOIN - 值可以为 NULL

javascript - 如何显示喜欢/不喜欢 MySQL、NodeJS

python - 如何在 redis 中正确使用连接池?

python - 使用对新/旧索引的容差在 Pandas 中重新索引数据框

python - 传递函数是 RPyC 中的参数

python - 在较大的字符串中查找嵌套序列

python - 使用 pandas 根据 groupby 将行中的多个变量转置为列

python - 你如何使用 curses 按下最后一个箭头键?