我是ios app开发的新手。这是我正在使用的sqlite数据库的更新代码,但它没有更新它显示数据库被锁定。任何帮助。
-(BOOL) updateSignalCode:(NSString*)deviceString buttonType:(NSString*)name
{
const char *dbPath=[databasePath UTF8String];
if (sqlite3_open(dbPath, &database)==SQLITE_OK) {
NSLog(@"database Opened");
sqlite3_busy_timeout(database, 800);
NSString *insertSQL = [NSString stringWithFormat:@"update deviceDetail Set deviceId = '%@' where buttontype = '%@'",deviceString,name];
const char *insert_stmt = [insertSQL UTF8String];
if (sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL)==SQLITE_OK) {
NSLog(@"Query Executed");
if (sqlite3_step(statement) == SQLITE_DONE) {
NSLog(@"Database Updated");
}
else {
NSLog(@"Error while updating.%d %s",sqlite3_errcode(database), sqlite3_errmsg(database));
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return nil;
}
答案 0 :(得分:0)
您需要将sqlite文件复制到appdelegate中的文档目录。
- (void)createCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSLog(@"%@",[self getDBPath]);
success = [fileManager fileExistsAtPath:[self getDBPath]];
if (success){
return;
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqlite"];// replace name of sqlite here
success = [fileManager copyItemAtPath:defaultDBPath toPath:[self getDBPath] error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
-(NSString *)getDBPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:@"db.sqlite"]; // replace name of sqlite here
return dbPath;
}
在Appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self createCopyOfDatabaseIfNeeded];
return YES;
}