热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Sqlite更新无法正常工作-python-Sqliteupdatedon'tworkingright-python

EDIT:aftersometestifoundoutthatitdontwastheaddpointmethodthatfailed.编辑:经过一些测试,我发现它

EDIT: after some test i found out that it don't was the addpoint method that failed.

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

I'm working on a small game to a irc bot. This method will update the score in database called 'score', the are only two players. It's a sqlite database. It's mostly the update sql that ain't working right.

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

Thanks

谢谢

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

and this is the way i created the score db

这就是我创建得分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()

error message: parameters are of unsupported type

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

3 个解决方案

#1


26  

Although the original author has most likely moved on, I figured I'd leave an answer here for future Googler's (like me ^_^).

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

I think what's happening here is that the following error...

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

ValueError: parameters are of unsupported type

ValueError:参数属于不受支持的类型

... is actually coming from the following line (contrary to what the author said).

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

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

The problem here is that Cursor.execute accepts the query string as the first parameter (which he has right), but a list, tuple, or dict as the second parameter. In this case, he needs to wrap that id in a tuple or list, like this:

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

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

A list or tuple can be used with positional arguments (which is when you use a question mark ? as the placeholder). You can also use a dict and :key for named arguments, as follows:

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

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

#2


2  

There is an error in your last select

您上次选择时出错

This

这个

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

must be written as

必须写成

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

#3


2  

You have another serious issue with your code assuming 'c' is a cursor. SQLite cursors get the next result row one at a time (ie each time through the for loop) rather than all in advance. If you reuse a cursor then it replaces the current query with a new one. For example this code will only run through the loop once:

假设'c'是游标,你的代码还有另一个严重问题。 SQLite游标一次获得一个下一个结果行(即每次通过for循环),而不是提前完成。如果重用游标,则会用新的查询替换当前查询。例如,此代码仅运行一次循环:

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

Your solutions include:

您的解决方案包括

  • Add .fetchall() on the end: c.execute("select * from score").fetchall() which gets all the rows up front rather than one at a time.

    在末尾添加.fetchall():c.execute(“select * from score”)。fetchall()获取前面的所有行而不是一次获取一行。

  • Use different cursors so the iteration through each one doesn't affect the others

    使用不同的游标,因此每个游标的迭代不会影响其他游标

  • Make a new cursor - replace c.execute("...") with conn.cursor().execute("...") Recent versions of pysqlite let you do conn.execute("...") which is effectively doing that above behind the scenes.

    创建一个新游标 - 用conn.cursor()替换c.execute(“...”)。execute(“...”)pysqlite的最新版本让你做conn.execute(“...”)这是在幕后有效地做到这一点。

Cursors are very cheap so do not try to conserve them - use as many as you want - and you won't have errors like this.

游标非常便宜,所以不要试图保存它们 - 使用尽可能多的游标 - 你就不会有这样的错误。

In general it is also a good idea to be very careful reusing iterators and modifying what you are iterating over within the same series of loops. Various classes behave in varying ways so it is best to assume they do not like it unless shown otherwise.

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


推荐阅读
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
author-avatar
小女人快乐方子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有