【问题标题】:Mysql Python: Not all parameters were used in MySQL statementMysql Python:并非所有参数都在MySQL语句中使用
【发布时间】:2017-07-25 20:09:58
【问题描述】:

所以我试图将数据作为变量输入到我的表中,但我似乎不断收到错误 mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement, I have used the %s instead of行名称,但我似乎不断收到相同的错误。我怀疑这是我的语法,但我似乎无法弄清楚这是我第一次同时使用 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_INCREMENT,

Sensor_ID VARCHAR (15) NOT NULL,

位置 VARCHAR (20) NOT NULL,

Sensor_IP VARCHAR (15) 非空,

Sensor_1 VARCHAR (15) 非空,

Sensor_1_Unit VARCHAR (15) NOT NULL,

Sensor_2 VARCHAR (15),

Sensor_2_Unit VARCHAR (15),

传感器_3 VARCHAR (15),

Sensor_3_Unit VARCHAR (15),

Time_Stamp TIMESTAMP NOT NULL,

主键 (ID));

【问题讨论】:

    标签: python mysql python-2.7


    【解决方案1】:

    您需要将 %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 中的示例。

    【讨论】:

      【解决方案2】:

      看起来您在插入语句中缺少formatting 您的实际变量。尝试使用一种已知的方法来格式化它们,%s 或 .format 方法。插入时,您也没有使用表中的时间戳、最后一列和值。如果您只是引用该表,您将有一个列不匹配。您必须明确说明要填充的列。您可以为此使用 CURRENT_TIMESTAMP,因为它对列描述说 NOT NULL。

      query = ("""Insert into iot_sensors (Sensor_ID, Location, Sensor_IP, Sensor_1, Sensor_1_Unit, Sensor_2, Sensor_2_Unit, Sensor_3, Sensor_3_Unit) values
                          ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}');""".format(Sensor_ID,Location,Sensor_IP,Sensor_1,Sensor_1_Unit,Sensor_2,Sensor_2_Unit,Sensor_3,Sensor_3_Unit))
      mycursor.execute(query)
      

      query = ("""Insert into iot_sensors (Sensor_ID, Location, Sensor_IP, Sensor_1, Sensor_1_Unit, Sensor_2, Sensor_2_Unit, Sensor_3, Sensor_3_Unit) values
          (%s,%s,%s,%s,%s,%s,%s,%s,%s);""" % (Sensor_ID,Location,Sensor_IP,Sensor_1,Sensor_1_Unit,Sensor_2,Sensor_2_Unit,Sensor_3,Sensor_3_Unit))
      mycursor.execute(query)
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2016-04-04
      • 2019-09-09
      相关资源
      最近更新 更多