试图设置时间和其他值以记录表,但显示:“字段列表”中的未知列“ time”,我将时间定义为VARCHAR(255)并使其特定于“ e”,因为它是char类型,在座的任何人都可以帮我解决这个问题,非常感谢
import mysql.connector
import datetime
d = datetime.datetime.now()
e = d.strftime("%Y-%m-%d %H:%M:%S")
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="asd619248636",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.excute=("CREATE TABLE record (temperature FLOAT(20) , humidity FLOAT(20), time VARCHAR(255))")
#mycursor.execute("SHOW TABLES")
#for x in mycursor:
#print(x)
sql = "INSERT INTO record (temperature,humidity,time) VALUES (%s, %s, %s)"
val = (3.2,6.5,"e")
mycursor.execute(sql,val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
答案 0 :(得分:0)
根据this
,我认为 时间 是mysql中的关键字因此,要将其用作列名,您需要将其用反引号括起来:
create_table_sql = "CREATE TABLE record (temperature FLOAT(20) , humidity FLOAT(20), `time` VARCHAR(255))"
sql = "INSERT INTO record (temperature,humidity,`time`) VALUES (%s, %s, %s)"