I am using sqlite database for store my data.But it is showing error of “database is locked” while insert query.Here is my code
sqlite3_stmt *statement;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
const char *dbpath = [[defaults objectForKey:@"dbpath"] UTF8String];
if (sqlite3_open(dbpath, &studentDB22) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO Emotion_videos (name) VALUES (\"%@\")",filePath];
const char *query_stmt = [insertSQL UTF8String];
if (sqlite3_prepare_v2(studentDB22, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(@"shi h2345");
if (sqlite3_step(statement) == SQLITE_ROW)
{
printf( "could not prepare statement: %s\n", sqlite3_errmsg(studentDB22));
}
}
sqlite3_finalize(statement);
}
else
{
printf( "could not prepare statement: %s\n", sqlite3_errmsg(studentDB22));
}
I am able to insert data for two or three times but when it runs again,it shows database locked error.
You open the database but you don’t close it.
You shouldn’t use string formats to create queries. You should put ? placeholders in the query then bind the proper value. This takes care of things like properly quoting and escaping string values.
After you prepare the statement, call
sqlite3_bind_textto bind the string value.Also. don’t use
sqlite3_open, usesqlite3_open_v2. It’s better and gives you more control.