如何在python中的sql查询中使用变量

时间:2018-06-09 17:49:21

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

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

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.*

有人可以告诉我它有什么问题吗?

1 个答案:

答案 0 :(得分:0)

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