Mysql Python:并非所有参数都在MySQL语句中使用

时间:2017-03-06 04:14:52

标签: python mysql python-2.7

所以我试图将数据输入到我的表中作为变量,但我似乎继续得到错误mysql.connector.errors.ProgrammingError:并非所有参数都在SQL语句中使用,我使用的是%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_INCREMENT,

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 TIMESTAMP非空,

PRIMARY KEY(ID));

2 个答案:

答案 0 :(得分: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中的示例。

答案 1 :(得分:1)

看起来你从insert语句中遗漏了formatting你的实际变量。尝试使用已知方法之一%s或.format方法格式化它们。插入时,您也没有使用表中的时间戳,最后一列,值。如果您只是引用该表,则列匹配不匹配。您必须明确说明要填充的列。您可以使用CURRENT_TIMESTAMP,因为它表示列描述为NOT NULL。

Slot

struct SlotPoint
{
    public readonly int X;
    public readonly int Y;

    public SlotPoint(int x, int y)
    {
        X = x; Y = y;
    }
}

const int _kcityWidth = 7;
const int _kcityHeight = 7;

SlotPoint ConvertCityToWorld(Slot slot)
{
    return new SlotPoint(
        slot.city.x * _kcityWidth + slot.x, slot.city.y * _kcityHeight + slot.y);
}