Reputation: 11
I am new in iOS development and recently I am facing some strange behavior on iOS SQLite. I have an application that allow user to insert record into the SQLite DB. The application work fine but after sometime the record created by the user somehow disappear by itself. When this happen, subsequently when I try to create a new record it does not store the record in DB. The insert statement executed without error but data is not insert. Below is the function that perform the record insert logic. Please help, thank you.
+ (BOOL)insertAutoRecord:(AutoRecord *)record{
BOOL ret = NO;
NSString *query = [NSString stringWithFormat:@"insert into %@ (%@, %@, %@, %@, %@, %@, %@) values ('%@', %d, %d, '%@', %d, %d, %d)",
TABLE_NAME_AUTOMATIC_RECORDS,
COL_NAME_AUTO_RECORD_SENSOR_ID,
COL_NAME_AUTO_RECORD_MONITOR_START_DATE,
COL_NAME_AUTO_RECORD_MONITOR_END_DATE,
COL_NAME_AUTO_RECORD_FREE_TEXT,
COL_NAME_AUTO_RECORD_CREATE_DATE,
COL_NAME_AUTO_RECORD_KID_ID,
COL_NAME_AUTO_RECORD_LAST_SYNC_DATE,
record.sensorId,
record.startDate,
record.endDate,
[record.freeText stringByReplacingOccurrencesOfString:@"'" withString:@"''"],
record.createDate,
record.kidId,
0];
KSDelegate *appDelegate = (KSDelegate *)[[UIApplication sharedApplication] delegate];
sqlite3 *dbHandler = appDelegate.dbHandler;
sqlite3_stmt *stmt;
const char *insert_stmt = [query UTF8String];
sqlite3_prepare_v2(dbHandler, insert_stmt, -1, &stmt, NULL);
if (sqlite3_step(stmt) == SQLITE_DONE)
{
ret = YES;
}
else
{
ret = NO;
}
sqlite3_finalize(stmt);
if(ret == NO)
return NO;
query = [NSString stringWithFormat:@"select last_insert_rowid()"];
if(sqlite3_prepare(dbHandler, [query UTF8String], -1, &stmt, nil) != SQLITE_OK)
return NO;
if(sqlite3_step(stmt) == SQLITE_ROW)
{
int recordId = (int)sqlite3_column_int(stmt, 0);
record.recordId = recordId;
}
else
ret = NO;
sqlite3_finalize(stmt);
return ret;
}
Upvotes: 0
Views: 301
Reputation: 7461
Here the perfect example and sample code of SQlite..I suggest you to go through it...
SQlite tutorial for iOS Developer
Upvotes: 1