Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 尝试将用户输入插入SQL表时出错_Postgresql_Python 2.7 - Fatal编程技术网

Postgresql 尝试将用户输入插入SQL表时出错

Postgresql 尝试将用户输入插入SQL表时出错,postgresql,python-2.7,Postgresql,Python 2.7,我遇到了一个问题,每当我尝试运行下面的代码并选择语句2以插入一个工具并输入一个随机工具名时,我都会收到错误消息:ProgrammingError('inserttool'列不存在\n第1行:插入工具(工具名,租赁天数)值(@inserttool,'2') 代码如下: if Menu == "2": cursor = connection.cursor() InsertTool = raw_input("Please insert the tool that you want to add

我遇到了一个问题,每当我尝试运行下面的代码并选择语句2以插入一个工具并输入一个随机工具名时,我都会收到错误消息:ProgrammingError('inserttool'列不存在\n第1行:插入工具(工具名,租赁天数)值(@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();

非常感谢,现在它工作得非常好!我感谢您的帮助!:)