How to insert data into table in sqlite iPhone?
I am trying following, but its failing:
NSString *query=[NSString stringWithFormat:@"insert into %@ (name) values ('%@')", table name,myName ];
sqlite3 *database;
sqlite3_stmt *createStmt = nil;
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
if (createStmt == nil) {
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &createStmt, NULL) != SQLITE_OK) {
return NO;
}
sqlite3_exec(database, [query UTF8String], NULL, NULL, NULL);
return YES;
}
return YES;
}else {
return NO;
}
I have created table in following manner:
create table if not exists myDets (dets_id integer primary key asc, name text);
I am also using Firefox SQLite plugin to check db. When I try to insert record via firefox into my db it gives me following error:
Failed to insert values
Exception Name: NS_ERROR_STORAGE_IOERR
Exception Message: Component returned failure code: 0x80630002 (NS_ERROR_STORAGE_IOERR) [mozIStorageStatement.execute]
Badly stuck 🙁
Also, i am running this on iPhone simulator, does it matter?
Please help
Thanks in advance.
Here’s an update function from a sample of mine:
In the sample, the db is copied from resources to a path and opened. Here’s the ensure opened function I use to do that: