为了账号安全,请及时绑定邮箱和手机立即绑定

通过 select ... where ... 命令使用 python.connector 时出错

通过 select ... where ... 命令使用 python.connector 时出错

胡说叔叔 2023-05-09 10:11:36
当我运行这个函数时发生了一些错误,但我不知道出了什么问题。def insertVariblesIntoTable(newepc, newtimestamp):    try:        connection = mysql.connector.connect(host='localhost',                                             database='myDB',                                             user='root',                                             password='root')        cursor = connection.cursor()        mySql_insert_query = """INSERT INTO myTB(epc,time_stamp)                                  SELECT newepc,newtimestamp                                    FROM dual                                    WHERE NOT EXISTS (SELECT * FROM myTB                                                        WHERE epc=newepc                                                         AND time_stamp>NOW()-INTERVAL 1 MINUTE )"""        cursor.execute(mySql_insert_query)        connection.commit()        print("Record inserted successfully into myTB table")    except mysql.connector.Error as error:        print("Failed to insert into MySQL table {}".format(error))    finally:        if (connection.is_connected()):            cursor.close()            connection.close()            # print("MySQL connection is closed")错误:无法插入 MySQL 表 1054 (42S22):“字段列表”中的未知列“newepc”
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

据我所知,您正在尝试访问一个不存在的名为“newepc”的列。发生这种情况是因为您没有在选择查询中转义变量“newepc”,mySql 不知道它应该是一个变量。您可以尝试 f-Strings 或只是正常的连接。



查看完整回答
反对 回复 2023-05-09
  • 1 回答
  • 0 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信