Sqlite更新无法正常工作 - python

Enu*_*mto 3 python sqlite

编辑:经过一些测试,我发现它不是失败的addpoint方法.

我正在为一个小型游戏开发一个irc机器人.这种方法将更新数据库中的得分称为"得分",这只是两个玩家.这是一个sqlite数据库.它主要是更新sql无法正常工作.

谢谢

def addpointo(phenny, id, msg, dude):
 try:
  for row in c.execute("select score from score where id = '0'"):
   for bow in c.execute("select score from score where id = '1'"):
    if int(row[0]) == 3:
     phenny.say("Winner is " + dude)
     clear("score") # clear db
     clear("sap") # clear db
    elif int(bow[0]) == 3:
     phenny.say("Winner is " + dude)
     clear("score") # clear db
     clear("sap") # clear db
    else:
     phenny.say(msg)
     s = c.execute("select score from score where id=?", id)
     a = int(s.fetchone()[0]) + 1
     print a
     c.execute("update score SET score =? where id =?", (a, id)) #here i got some prolem
     conn.commit()
 except Exception:
  phenny.say("Error in score. Try to run '.sap clear-score' and/or '.sap clear-sap'")
  pass
Run Code Online (Sandbox Code Playgroud)

这就是我创建得分db的方式

def createscore():
 if not (checkdb("score") is True):
  c.execute('''create table score (id int, score int)''')
  c.execute('insert into score values (0, 0)')
  conn.commit()
  c.execute('insert into score values (1, 0)')
  conn.commit()
Run Code Online (Sandbox Code Playgroud)

错误消息:参数属于不受支持的类型

Joe*_*gen 26

虽然原作者很可能继续前进,但我想我会在这里为未来的Googler留下答案(就像我^ _ ^).

我想这里发生的是以下错误......

ValueError: parameters are of unsupported type

......实际上来自以下几行(与作者所说的相反).

s = c.execute("select score from score where id=?", id)
Run Code Online (Sandbox Code Playgroud)

这里的问题是,Cursor.execute接受查询字符串作为第一个参数(他具备正确的),但是list,tupledict作为第二个参数.在这种情况下,他需要将它包装id在元组或列表中,如下所示:

s = c.execute("select score from score where id=?", (id,))
Run Code Online (Sandbox Code Playgroud)

列表或元组可以与位置参数一起使用(当您使用问号?作为占位符时).您还可以使用a dict:keyfor命名参数,如下所示:

s = c.execute("select score from score where id=:id", {"id": id})
Run Code Online (Sandbox Code Playgroud)

  • 感谢我们googlers的答案!! 这解决了我的问题!! (4认同)