Python 3.5 sqlite3传递字符串参数 - 提供的绑定数量不正确

时间:2016-07-14 05:07:59

标签: python sqlite

错误:

  

c = dbConnection.execute(" SELECT compid FROM" + tableToUse +" WHERE   id =?",id)       sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用1,并且提供了2个。

当我这样做时:

def getcompid (dbConnection, tableToUse, id):
    c = dbConnection.execute("SELECT compid FROM " + tableToUse + " WHERE id = ?", id)

manualcompid = [('8','from01'),('35','28')]
for manid in manualcompid:

    ERROR:
    foundid = getcompid (dbConnection, tableToUse, manid[0])

    OR SAME ERROR:

    r = str(manid[0])
    foundid = getcompid (dbConnection, tableToUse, r)


    THE BELOW IS FINE:
    foundid = getcompid (dbConnection, tableToUse, '8')

它对我的接触r应该是简单的' string与' 8'相同,更多manid[0]已经是字符串。为什么错误?

为什么我必须使用:foundid = getcompid (dbConnection, tableToUse, (manid[0],))

1 个答案:

答案 0 :(得分:1)

绑定需要在列表或元组中。试试这个:

c = dbConnection.execute(
    "SELECT compid FROM " + tableToUse + " WHERE id = ?", [id])