“库例程调用不连续”sqlite3_prepare_v2(CREATE TABLE)

前端之家收集整理的这篇文章主要介绍了“库例程调用不连续”sqlite3_prepare_v2(CREATE TABLE)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我在空数据库调用sqlite3_prepare_v2(CREATE TABLE)时,你知道为什么我会得到“Library Routine Called Out of Sequence”吗?

我创建一个空数据库,然后打开它.后来我保存了所有必须写入RAM中的数据库的信息(我需要将该信息保存在RAM中并在执行结束时将其刷新到永久存储器),但是当我调用sqlite3_prepare_v2(创建表)时,我收到此错误消息).它返回“Library Routine Called Out of Sequence”作为错误消息.

我确实正确地打开了我的数据库(我认为这可能是一个问题,所以我关闭()我的数据库然后在调用sqlite3_prepare_v2(CREATE TABLE)之前打开().我认为这可能是因为线程并发,但使用临界区也没有帮助.

这就是 documentation所说的错误原因:
  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.

你提到尝试一个关键部分,所以我想我们可以排除#2.您的错误调用sqlite3_prepare_v2(…)而不是sqlite3_step()或sqlite3_bind()的结果,所以我猜这只会留下#1?你能仔细检查一下你的db指针好吗?将其追溯到返回它的sqlite3_open()并确保在调用prepare之前没有关闭它?

这对我有用:

#include <stdio.h>
#include <sqlite3.h>

int main(int argc,char **argv){

    sqlite3 *db;
    int rc;

    char *db_name= ":memory:";

    rc = sqlite3_open(db_name,&db);

    if (rc != sqlITE_OK) {
        fprintf(stderr,"Failed to open in memory database: %s\n",sqlite3_errmsg(db));
        sqlite3_close(db);
        return(1);
    }

    const char *create_sql = "CREATE TABLE foo(bar TEXT)";
    sqlite3_stmt *statement;

    rc = sqlite3_prepare_v2(db,create_sql,-1,&statement,NULL);

    if (rc != sqlITE_OK) {
        fprintf(stderr,"Failed to prepare statement: %s\n",sqlite3_errmsg(db));
        sqlite3_close(db);
        return(1);
    }

    rc = sqlite3_step(statement);

    if (rc == sqlITE_ERROR) {
        fprintf(stderr,"Failed to execute statement: %s\n",sqlite3_errmsg(db));
    }

    sqlite3_close(db);

}
原文链接:https://www.f2er.com/sqlite/197719.html

猜你在找的Sqlite相关文章