1. 程式人生 > >解決SQLite異常:library routine called out of sequence

解決SQLite異常:library routine called out of sequence

在平常的練習中出現了這樣的問題,在網上搜尋了下,發現沒有什麼確定的答案。

卻發現:

Error Code SQLITE_MISUSE (21) "Library routine called out of sequence"

The SQLITE_MISUSE error code is returned when you misuse the SQLitelibrary in some way. SQLite does not guarantee that it will detectmisuse, so you should not depend on this behavior in any way. TheSQLITE_MISUSE error code is intended to help you find the bugs inyour code.

Here are some possible causes of SQLITE_MISUSE:

  1. Calling any API routine with an sqlite3* pointer that was not obtained from sqlite3_open() or sqlite3_open16() or which has already been closed by sqlite3_close().
  2. Trying to use the same database connection at the same instant in time from two or more threads.
  3. Calling sqlite3_step() with a sqlite3_stmt* statement pointer that was not obtained from sqlite3_prepare() or sqlite3_prepare16() or that has already been destroyed by sqlite3_finalize().
  4. Trying to bind values to a statement (using sqlite3_bind_...()) while that statement is running.

這是SQLite官網上給出的問題解釋。

1.  呼叫API所用到的指標,第一種情況是沒有從sqlite3_open()或者是sqlite3_open16()獲得,第二種情況是sqlite3_open()函式已經將資料庫關閉了。我是第二種情況。

2.  兩個或者更多的執行緒同時訪問該資料庫。對於這樣的問題,可以通過加上鎖進行解決。

3.  sqlite3_step()所用到的變數statement指標,第一種情況是該指標不是從sqlite3_prepare()或者是sqlite3_open16()獲得的,第二種情況是該指標已經被銷燬或者被釋放。這個和1中的情況是一樣的,不同的是使用這種指標的函式,兩者可以和為一種情況。

4.  試圖將values繫結到一個正在執行的statement上。該解釋未遇到。