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

标签 postgresql python-2.7

我遇到了问题,每当我尝试运行下面的代码并选择语句 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/

相关文章:

sql - 查询计划 : Is the order of JOINS important

sql - 重命名 PostgreSQL 中的多个列

Python:导入matplotlib但无法使用imshow

python-2.7 - Python 类型错误 : can only concatenate list (not "str") to list

sql - LEFT JOIN 使用子查询不返回空结果为 "0"

postgresql - Postgres 窗函数计算基于二集

postgresql - Glassfish 2.1 从不重用 Postgresql 和 Eclipselink 的语句?

python - 用对自身的引用替换类中的方法

python-2.7 - 如何动态地将触发器 s3 存储桶添加到 lambda 函数(python boto3 API)

python打印转义反斜杠