psycopg2.ProgrammingError: 列 "your name" 不存在。

3

当我尝试使用以下代码上传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,“your name”是变量Player_Name。但是,当我在PostgreSQL查询工具中运行它时,它可以正常工作。有人知道为什么会出现这个错误,并且如何防止将来再次发生吗?
1个回答

5

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()

应该可以工作。它还可以接受一个值的元组。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接