Why am I getting SQLITE_MISUSE error: memory error?

I am writing an iOS application that directly accesses SQLite. I have done this many times on Android, so I try to figure out where my error is, but my inserts return an error SQLITE_MISUSE(code 21) with the message "out of Memory". Below are the steps that I have taken to lead me to this insert.

Firstly, creating a table :

NSString *sql = @"CREATE TABLE IF NOT EXISTS UsersTable (lastName TEXT,id TEXT PRIMARY KEY NOT NULL,picture BLOB,firstName TEXT,age TEXT,email TEXT,sex TEXT,height TEXT,weight TEXT)";

//create the database if it does not yet exist
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: path ] == NO)
{
    const char *dbpath = [path UTF8String];

    //This was if (sqlite3_open(dbpath, &store) == SQLITE_OK) , but it has not made a difference.
    if (sqlite3_open_v2(dbpath, &store, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) == SQLITE_OK)
    {
        char *errMsg = NULL;
        const char *sql_stmt = [sql UTF8String];

        if (sqlite3_exec(store, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
        {
            NSLog(@"Failed to create table: %s", errMsg);
        }
        if (errMsg)
            sqlite3_free(errMsg);
    }
    else
    {
        NSLog(@"Failed to open/create database");
    }
}

Then insert (currently using email address for user id):

INSERT INTO UsersTable (id,lastName,firstName,email) VALUES ("jsmith@foobar.com","Smith","John","jsmith@foobar.com")

I use one selector for all interactions with the database, so the above text is passed here:

-(int)execSQL:(NSString *)statement
{
    NSLog(@"%@",statement);

    const char *insert_stmt = [statement UTF8String];
    sqlite3_stmt *stmnt;

    sqlite3_prepare_v2(store, insert_stmt, -1, &stmnt, NULL);
    int result = sqlite3_step(stmnt);

    sqlite3_finalize(stmnt);

    if (result != SQLITE_OK)
    {
        NSLog(@"Error: %s", sqlite3_errmsg(store));//This prints "Error: out of memory"
    }
    return result;
}

What am I doing wrong?

+2
2

, . , , .

, SQLite , SQLITE_MISUSE ( , SQLite ), sqlite3_errmsg " ".


, :

  • sqlite3_prepare:

    - (int)execSQL:(NSString *)statement
    {
        int result;
    
        NSLog(@"%@",statement);
    
        const char *insert_stmt = [statement UTF8String];
        sqlite3_stmt *stmnt;
    
        if ((result = sqlite3_prepare_v2(store, insert_stmt, -1, &stmnt, NULL)) != SQLITE_OK)
        {
            NSLog(@"%s: prepare failure '%s' (%d)", __FUNCTION__, sqlite3_errmsg(store), result);
            return result;
        }
    
        if ((result = sqlite3_step(stmnt)) != SQLITE_DONE)
        {
            NSLog(@"%s: step failure: '%s' (%d)", __FUNCTION__, sqlite3_errmsg(store), result);
        }
    
        sqlite3_finalize(stmnt);
    
        return result;
    }
    

    , SQL, sqlite3_prepare_v2.

  • SQL NSString. SQL- , , SQL, - , . The "Destroyer". ? , sqlite3_bind_xxx . - :

    - (int)insertIdentifier:(NSString *)identifier
                   lastName:(NSString *)lastName
                  firstName:(NSString *)firstName
                      email:(NSString *)email
    {
        int result;
    
        const char *insert_stmt = "INSERT INTO UsersTable (id, lastName, firstName, email) VALUES (?, ?, ?, ?);";
        sqlite3_stmt *stmnt;
    
        if ((result = sqlite3_prepare_v2(store, insert_stmt, -1, &stmnt, NULL)) != SQLITE_OK)
        {
            NSLog(@"%s: prepare failure '%s' (%d)", __FUNCTION__, sqlite3_errmsg(store), result);
            return result;
        }
    
        if ((result = sqlite3_bind_text(stmnt, 1, [identifier UTF8String], -1, NULL)) != SQLITE_OK)
        {
            NSLog(@"%s: bind #1 failure '%s' (%d)", __FUNCTION__, sqlite3_errmsg(store), result);
            sqlite3_finalize(stmnt);
            return result;
        }
    
        if ((result = sqlite3_bind_text(stmnt, 2, [lastName UTF8String], -1, NULL)) != SQLITE_OK)
        {
            NSLog(@"%s: bind #2 failure '%s' (%d)", __FUNCTION__, sqlite3_errmsg(store), result);
            sqlite3_finalize(stmnt);
            return result;
        }
    
        if ((result = sqlite3_bind_text(stmnt, 3, [firstName UTF8String], -1, NULL)) != SQLITE_OK)
        {
            NSLog(@"%s: bind #3 failure '%s' (%d)", __FUNCTION__, sqlite3_errmsg(store), result);
            sqlite3_finalize(stmnt);
            return result;
        }
    
        if ((result = sqlite3_bind_text(stmnt, 4, [email UTF8String], -1, NULL)) != SQLITE_OK)
        {
            NSLog(@"%s: bind #4 failure '%s' (%d)", __FUNCTION__, sqlite3_errmsg(store), result);
            sqlite3_finalize(stmnt);
            return result;
        }
    
        if ((result = sqlite3_step(stmnt)) != SQLITE_DONE)
        {
            NSLog(@"%s: step failure: '%s'", __FUNCTION__, sqlite3_errmsg(store));
        }
    
        sqlite3_finalize(stmnt);
    
        return result;
    }
    

    :

    [self insertIdentifier:@"jsmith@foobar.com"
                  lastName:@"Smith"
                 firstName:@"John"
                     email:@"jsmith@foobar.com"];
    
  • , , , .., SQLite . FMDB. , SQLite, SQLite Objective-C.

+16

sqlite3_prepare_v2. SQLITE_OK, .

, ? , .

0

Source: https://habr.com/ru/post/1540141/


All Articles