Jas*_*rMW 2 python postgresql psycopg2 python-3.x
当我尝试上传PlayerScore代码时
if Number.PlayerScore > Score.interact_database("SELECT Score FROM score WHERE Name = %s;" % Player_Name, False,)[0][0]
Run Code Online (Sandbox Code Playgroud)
到 Python 中的 PostgreSQL 数据库时,我收到此错误:psycopg2.ProgrammingError: column "your name" does not exist,其中“您的姓名”是变量 Player_Name。但是,当我在 PostgreSQL 查询工具中运行它时,它运行良好。有谁知道为什么会弹出这个错误以及我如何防止它在未来再次发生?
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()
Run Code Online (Sandbox Code Playgroud)
应该管用。它还可以接受一组值。
| 归档时间: |
|
| 查看次数: |
5619 次 |
| 最近记录: |