编辑:经过一些测试,我发现它不是失败的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
这就是我创建得分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()
解决方法
虽然原作者很可能继续前进,但我想我会在这里为未来的Googler留下答案(就像我^ _ ^).
我想这里发生的是以下错误……
ValueError:参数属于不受支持的类型
……实际上来自以下几行(与作者所说的相反).
s = c.execute("select score from score where id=?",id)
这里的问题是Cursor.execute接受查询字符串作为第一个参数(他有权),但是列表,元组或dict作为第二个参数.在这种情况下,他需要将该id包装在元组或列表中,如下所示:
s = c.execute("select score from score where id=?",(id,))
列表或元组可以与位置参数一起使用(当您使用问号?作为占位符时).您还可以使用dict和:key作为命名参数,如下所示:
s = c.execute("select score from score where id=:id",{"id": id})