带变量

时间:2017-06-06 03:46:54

标签: python mysql

testid = 69
query = """SELECT * FROM basic_info WHERE ownerid=%s"""
cur.execute(query, testid)
print cur.fetchone()

尝试此操作时出现整数错误。我试过把testid转换成一个没有运气的字符串。

2 个答案:

答案 0 :(得分:0)

我更喜欢这种插入参数的方法,因为它很清楚:

query = "SELECT * FROM basic_info WHERE ownerid = %(testid)s"
cur.execute(query, params = { 'testid': testid })

答案 1 :(得分:0)

Cursor.execute的第二个参与者应该是序列(tuplelist)或映射(dict):

testid = 69
query = """SELECT * FROM basic_info WHERE ownerid=%s"""
cur.execute(query, [testid])  # <--
print cur.fetchone()