psycopg2.ProgrammingError:column"你的名字"不存在

时间:2017-01-23 10:33:18

标签: python postgresql python-3.x psycopg2

当我尝试使用代码

上传PlayerScore
if Number.PlayerScore > Score.interact_database("SELECT Score FROM score WHERE Name = %s;" % Player_Name, False,)[0][0]

使用Python中的PostgreSQL数据库我收到此错误:psycopg2.ProgrammingError: column "your name" does not exist,其中'您的姓名'是变量Player_Name。但是,当我在PostgreSQL查询工具中运行它时,它工作正常。有谁知道为什么会出现这种错误以及如何防止它在将来再次发生?

1 个答案:

答案 0 :(得分:4)

http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters

我认为我们需要查看更多代码,但根据文档:

  

从不,永远不要使用Python字符串连接(+)或字符串参数插值(%)将变量传递给SQL查询字符串。甚至在枪口下也没有。

尝试将参数作为cursor.execute()方法中的第二个参数传递。使用类似的东西:

cursor.execute("""SELECT Score FROM score WHERE Name = %(player)s""", {'player': player_name } )
cursor.fetchone()

应该有效。它也可以接受一组值。