how to save the data in sqlite3 in iphone

Your putting string quotes around your int params in your sql statement.

This may not be your only issue but you should bind params. It looks like you’re putting quotes around integer values in the sql insert statement which are defined as integers in your table.

"create table if not exists Feeback details (Traineeid integer, Trainername text,Traineename text,Rating float)"

"insert into Feedbackdetails(Traineeid,Trainername,Traineename,Rating) values(\"%d\",\"%@\", \"%@\", \"%@\")"

Notice your double quotes around ints.

Also, log out the path to your database (even if running in simulator). Go to the sqlite cmd line and ensure the db exists and the empty table is there. This helps in troubleshooting.

Finally, take a look @ the fmdb sqlite wrapper – it helps using sqlite but it’s code also shows good patterns for using sqlite raw if that’s your preference.

Here’s a similar function from one of my samples which shows how to bind params. You should also finalize what you prepare:

- (void)updateContact: (Contact*)contact error:(NSError**)error
{
    if (![self ensureDatabaseOpen:error])
    {
        return;
    }

    NSLog(@">> ContactManager::updateContact");

    // prep statement
    sqlite3_stmt    *statement;
    NSString *querySQL = @"update contacts set name=?,address=?,phone=? where id=?";
    NSLog(@"query: %@", querySQL);
    const char *query_stmt = [querySQL UTF8String];

    // preparing a query compiles the query so it can be re-used.
    sqlite3_prepare_v2(_contactDb, query_stmt, -1, &statement, NULL);     
    sqlite3_bind_text(statement, 1, [[contact name] UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_text(statement, 2, [[contact address] UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_text(statement, 3, [[contact phone] UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_int64(statement, 4, [[contact id] longLongValue]);

    NSLog(@"bind name: %@", [contact name]);
    NSLog(@"bind address: %@", [contact address]);
    NSLog(@"bind phone: %@", [contact phone]);
    NSLog(@"bind int64: %qi", [[contact id] longLongValue]);

    // process result
    if (sqlite3_step(statement) != SQLITE_DONE)
    {
        NSLog(@"error: %@", sqlite3_errmsg(_contactDb));
    }

    sqlite3_finalize(statement);
}

Leave a Comment