这是我用来Appdelegate.m
复制数据库的代码
- (void) copyDatabaseIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"LocalSongs.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
这用于获取 DBPath
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"LocalSongs.sqlite"];
}
这是我的播放列表插入方法
-(NSString *)InsertPlaylist :(NSString *)PlaylistName
{
NSLog(@"passed");
NSString *status;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath=[[NSString alloc]initWithString:[documentsDir stringByAppendingPathComponent:@"LocalSongs.sqlite"]];
NSLog(@"Database Path %@",dbPath);
sqlite3_stmt *statement;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSLog(@"open");
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO LOCALPLAYLIST (PLAYLISTNAME,getdate()) VALUES (\"%@\")",PlaylistName];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
status=@"Playlist Created";
}
else
{
status=@"Error occured";
}
return status;
}
}
我遇到的问题是这个 prepare_v2 总是没有完成。它总是执行 else 部分。
if (sqlite3_step(statement) == SQLITE_DONE)
这有什么问题?请帮我