【问题标题】:Sqlite update don't working right - pythonSqlite 更新无法正常工作 - python
【发布时间】:2012-07-17 23:59:25
【问题描述】:

编辑:经过一些测试,我发现它不是失败的 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()

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

【问题讨论】:

  • “工作不正常”是什么意思?你遇到了什么错误?
  • 你看过 sqlite 模块吗?此外,您应该阅读使用 sql。您的设计明显缺乏对使用数据库的意义的理解。
  • 我只得到这个错误:参数的类型不受支持
  • @Falmarri 是的,我以前使用过 sql,但从未使用过 python,但有时必须是第一次

标签: python sqlite


【解决方案1】:

虽然原作者很可能已经离开,但我想我会在这里为未来的 Google 员工(比如我 ^_^)留下一个答案。

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

ValueError: parameters are of unsupported type

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

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

这里的问题是Cursor.execute接受查询字符串作为第一个参数(他是正确的),但是listtupledict作为第二个参数。在这种情况下,他需要将 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})

【讨论】:

  • 感谢我们谷歌员工的答案!这解决了我的问题!
【解决方案2】:

你上次的选择有错误

这个

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

必须写成

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

【讨论】:

    【解决方案3】:

    假设 'c' 是一个游标,您的代码还有另一个严重问题。 SQLite 游标一次获取下一个结果行(即每次通过 for 循环),而不是提前获取所有结果。如果您重用游标,那么它将用新的查询替换当前查询。例如这段代码只会在循环中运行一次:

    for row in c.execute("select * from score"):
       for dummy in c.execute("select 3"):
          print row, dummy
    

    您的解决方案包括:

    • 在末尾添加 .fetchall():c.execute("select * from score").fetchall() 将所有行放在前面,而不是一次一行。

      李>
    • 使用不同的游标,这样每个游标的迭代都不会影响其他游标

    • 创建一个新游标 - 将 c.execute("...") 替换为 conn.cursor().execute("...") 最近版本的 pysqlite 允许您执行 conn.execute("...") ,这实际上是在幕后执行上述操作。

    游标非常便宜,因此不要试图保存它们 - 尽可能多地使用 - 这样就不会出现这样的错误。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 2023-04-07
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      相关资源
      最近更新 更多