Sqlite更新无法正常工作-python

Sqlite更新无法正常工作-python,python,sqlite,Python,Sqlite,编辑:经过一些测试,我发现addpoint方法并没有失败 我正在做一个irc机器人的小游戏。此方法将更新名为“score”的数据库中的分数,只有两名玩家。这是一个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

编辑:经过一些测试,我发现addpoint方法并没有失败

我正在做一个irc机器人的小游戏。此方法将更新名为“score”的数据库中的分数,只有两名玩家。这是一个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
这就是我创建分数数据库的方式

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

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

上次选择时出错

这个

必须写为

s = c.execute("select score from score where id=?", id)

假设“c”是游标,那么代码还有另一个严重问题。SQLite游标一次一行地获取下一个结果,即每次都通过for循环,而不是预先获取所有结果。如果重用游标,则它将用新查询替换当前查询。例如,此代码只在循环中运行一次:

for row in c.execute("select * from score"):
   for dummy in c.execute("select 3"):
      print row, dummy
您的解决方案包括:

在末尾添加.fetchall:c.executeselect*from score.fetchall,它将在前面获取所有行,而不是一次一行

使用不同的游标,这样通过每个游标的迭代不会影响其他游标

创建一个新游标-替换c.execute。。。使用conn.cursor.execute。。。 pysqlite的最新版本允许您执行conn.execute。。。这实际上是在幕后进行的

游标非常便宜,所以不要试图保存它们——可以使用任意数量的游标——这样就不会出现这样的错误


一般来说,非常小心地重用迭代器和修改在同一系列循环中迭代的内容也是一个好主意。不同的类的行为方式各不相同,因此最好假设它们不喜欢,除非另有说明。

尽管最初的作者很可能已经离开了,但我想我会在这里给未来的谷歌人留下一个答案

我认为这里发生的是以下错误

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和:键,如下所示:

s = c.execute("select score from score where id=:id", {"id": id})

你说工作不正常是什么意思?您得到了什么错误?您是否查看过sqlite模块?此外,您还应该了解sql的使用。您的设计显然不了解使用数据库的意义。我只得到这个错误:参数不受支持type@Falmarri是的,我以前用过sql,但从来没有用过python,但有时肯定是第一次感谢我们谷歌人的答案!!这解决了我的问题!!
s = c.execute("select score from score where id=:id", {"id": id})