SQLite3 (iOS) query fails

I have problems getting this to work.

This is the code I want to execute:

[self openDB];
sqlite3_stmt *statement;
NSString *sql2 = [NSString stringWithFormat:@"INSERT INTO CheckList (CLName, DateAdd, Active, Costum, Percentage, UserId) VALUES ('ola232332332324', '2012-02-03', 1, 1, NULL, 1)"];
if(sqlite3_prepare_v2(db, [sql2 UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
    alert1 = [[UIAlertView alloc]
              initWithTitle:@"Grats" message:@"The query was executed!" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles: nil];
    [alert1 show];
    [alert1 release];
} 
else {
    NSAssert(0, @"Failed to execute");
}
sqlite3_finalize(statement);

And this is the code that I use to call the database:

-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    return [documentsDir stringByAppendingPathComponent:@"prosegur.sqlite"];
}

-(void) openDB {
    sqlite3_open([[self filePath] UTF8String], &db);
    if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK){
        NSAssert(0, @"Failed to open db");
    }
}

Edit: trying to use FMDB, here is the code:

FMDatabase *database = [FMDatabase databaseWithPath:@"prosegur.sqlite"];
if([database open]){
    NSLog(@"yes");
    [database executeUpdate:@"INSERT INTO CheckList (CLName, DateAdd, Active, Costum, UserId) VALUES ('cenas', '2012-02-0',  1, 1, 1)"];}
else
{
    NSLog(@"not");
}

Passes the test [open the database], but does not fulfill the request.

EDIT 2: Already done what was suggested, I'm starting to think that he creates the database and executes the query inside it, but since it does not have a table, it ignores this fact.

Here is the updated code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [[paths objectAtIndex:0] retain];
NSString *path = [[docsPath stringByAppendingPathComponent:@"prosegur.sqlite"] retain];

FMDatabase *database = [FMDatabase databaseWithPath:path];
[database open];
if([database open])
{
    NSLog(@"yes");
    [database executeUpdate:@"INSERT INTO CheckList (CLName) VALUES (?)", @"cenas"];
}
else 
{
    NSLog(@"no");
}
[database close]; 
+3
source share
4 answers
0

, , , sqlite3_prepare_v2 ? sqlite3_step, ,

sqlite3_exec, /.

+2

sqlite3 api iOS. FMDB. , .

, NSError .

+2

.

+(BOOL)InsertDataInPin:(NSString *)pin
    {
        NSString *databasePath =Your DatabasePath;

        NSString *SQL=@"INSERT INTO pincode Values(?)";
        sqlite3_stmt *dataset;


        if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK ) {

            if (sqlite3_prepare_v2(database, [SQL UTF8String], -1, &dataset, NULL) == SQLITE_OK) 
            {

                sqlite3_bind_text(dataset, 1, [pin UTF8String], -1, SQLITE_TRANSIENT);
                sqlite3_step(dataset);

            }       

            sqlite3_close(database);
        }


    }
+1
source

All Articles