gpt4 book ai didi

postgresql - 尝试将用户的输入插入 SQL 表时出错

转载 作者:行者123 更新时间:2023-11-29 13:07:38 24 4
gpt4 key购买 nike

我遇到了问题,每当我尝试运行下面的代码并选择语句 2 以插入工具 并输入随机工具名称时,我都会收到错误消息:ProgrammingError ('column "inserttool' does not exist\nLine 1: insert tools(tool_name, rental_days) values(@insertTool, '2') .

代码如下:

if Menu == "2":
cursor = connection.cursor()
InsertTool = raw_input("Please insert the tool that you want to add.\n")
insert_tool = """insert into tools(tool_name, rental_days) values(@InsertTool, '2')"""
try:
cursor.execute( insert_tool);
connection.commit();
print("Tool is succesfully inserted!")

except Exception as e:
connection.rollback();
print("Exception Occured : ",e)
connection.close();

最佳答案

试试这个。

if Menu == "2":
cursor = connection.cursor()
InsertTool = raw_input("Please insert the tool that you want to add.\n")
insert_tool = """insert into tools(tool_name, rental_days) values(%s, %s)"""
val = (InsertTool, "2")
try:
cursor.execute(insert_tool, val);
connection.commit();
print("Tool is succesfully inserted!")

except Exception as e:
connection.rollback();
print("Exception Occured : ",e)
connection.close();

关于postgresql - 尝试将用户的输入插入 SQL 表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59099440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com