Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1683029
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2011-09-05 09:12:55

1、我的程序中的一个类负责链接数据库并对数据库进行操作,使用中我全部使用默认链接,就是说下一个操作会的链接会自动替换掉现在的链接,所以每一个函数 中链接都是新的,只要QSqlDatabase和QSqlQuery都重置就什么问题都没有了。但是事实证明我错了。这个类中我先是使用了一些查询,然后 再想update的时候就会报错“database is locked”

解决方法:其实因为我的QSqlDatabase和QSqlQuery是数据成员,所以在函数之间它们不会真正失效。上一个成功的select会将 QSqlQuery设置为isActive,如果一直不通知Qt,QSqlQuery用完了,它将一直保持活跃。所以在Update时QSqlQuery 认为自己的上一次操作,即select还没有完成,就会报错说database is locked.

解决方法来源:

Error Code SQLITE_LOCKED (6): Database Is Locked

This error code occurs when you try to do two incompatible things with a database at the same time from the same database connection. For example, if you are in the middle of a SELECT statement and you try to DROP one of the tables being read by the SELECT, you will get an SQLITE_LOCKED error. Here is an example (using ):

db eval {SELECT rowid FROM ex1} {
if {$rowid==10} {
db eval {DROP TABLE ex1} ;# will give SQLITE_LOCKED error
}
}

Note that an SQLITE_LOCKED error is distinct from SQLITE_BUSY (5). SQLITE_BUSY means that another database connection (probably in another process) is using the database in a way that prevents you from using it. SQLITE_LOCKED means the source of contention is internal and comes from the same database connection that received the SQLITE_LOCKED error.

Here are other reasons for getting an SQLITE_LOCKED error:

  1. Trying to CREATE or DROP a table or index while a SELECT statement is still pending.
    • Sometimes people think they have finished with a SELECT statement because sqlite3_step() has returned SQLITE_DONE. But the SELECT is not really complete until sqlite3_reset() or sqlite3_finalize() have been called.
    • As of check-in [3902] (2007-05-02 after version 3.3.17) this is now allowed for CREATE statement.
  2. Trying to write to a table while a SELECT is active on that same table.
    • As of check-in [3355] (2006-08-16 after version 3.3.7) this is now allowed.
  3. Trying to do two SELECT on the same table at the same time in a multithread application, if sqlite is not set to do so. Anonymous adds: can someone please expand on the 'not set to do so' above? DRH replys: This third bullet item was added anonymously. I am not aware of any restrictions on doing two or more SELECTs from different threads at the same time, as long as each SELECT is happening in an independent database connection (that is: a separate sqlite3* object obtained from separate calls). As far as I know, the only time you can get an SQLITE_LOCKED error in the latest code is according to the first bullet above.
在Qt中体现为QSqlQuery使用完成后,要用finish()函数或clear()函数通知它操作已经完成,使之不再活跃。
阅读(953) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~