psycopg2.ProgrammingError:列“your name”不存在

2024-05-14 03:48:28 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图上传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\u name。但是,当我在PostgreSQL查询工具中运行它时,它工作得很好。有人知道为什么会出现这样的错误吗?我怎样才能防止它在将来再次发生?在


Tags: 代码namefromnumberyourifpostgresql错误
1条回答
网友
1楼 · 发布于 2024-05-14 03:48:28

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

我想我们需要更多的代码,但是根据文档:

Never, never, NEVER use Python string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. Not even at gunpoint.

尝试将参数作为光标.执行()方法。使用类似于:

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

应该行得通。它还可以接受值的元组。在

相关问题 更多 >