【问题标题】:How to use variables in sql query in python如何在python的sql查询中使用变量
【发布时间】:2018-06-09 17:49:21
【问题描述】:

我正在尝试创建一个银行系统及其我定义的功能之一

def fetch(temp_pass,temp_accno):
    cur.execute("SELECT  id,name,acc_no,ph_no,address,email,balance from accounts where id = %s and acc_no = %s",(temp_pass,temp_accno,))
    row = cur.fetchall();
    return row

这是我得到的错误

*\Traceback (most recent call last):
  File "Accounting.py", line 126, in <module>
    deposit()
  File "Accounting.py", line 79, in deposit
    fetch(temp_pass,temp_accno)
  File "Accounting.py", line 8, in fetch
    cur.execute("SELECT  id,name,acc_no,ph_no,address,email,balance from accounts where id = %s and acc_no = %s",(temp_pass,temp_accno,))
psycopg2.ProgrammingError: operator does not exist: character = integer
LINE 1: ...h_no,address,email,balance from accounts where id = 1234 and...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.*

谁能告诉我它有什么问题?

【问题讨论】:

  • 是 temp_pass,temp_accno 是 int 值或字符串值,还要去掉 temp_pass,temp_accno, 后多余的逗号 , 应该是 temp_pass,temp_accno
  • 它们是 int 值
  • 你试过去掉多余的逗号吗?
  • 另外,试试这个cur.execute("SELECT id,name,acc_no,ph_no,address,email,balance from accounts where id = " + temp_pass + " and acc_no = " + temp_accno)
  • 那没用,但我现在正确了

标签: python-3.x psycopg2 postgresql-9.5


【解决方案1】:
cur.execute('''SELECT id, name, acc_no, ph_no,address, email,balance
               FROM accounts
               WHERE id = %s and acc_no = %s''',
            (str(temp_pass), str(temp_accno)));

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多