我遇到了一个问题,每当我试图运行下面的代码并选择语句2来插入一个工具并放置一个随机工具名时,我就得到了错误消息:ProgrammingError(“列”插入工具)不存在\nLINE 1:插入工具(TooTyNoRead,RunalalStEd)值(@插入工具,‘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/

10-15 19:46