这是我在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)
这有什么问题?请帮帮我
答案 0 :(得分:1)
我认为您的查询有误:您应该在您放置getdate()
的位置添加列名,即代替
INSERT INTO LOCALPLAYLIST (PLAYLISTNAME,getdate()) VALUES (\"%@\")
你应该使用像
这样的东西INSERT INTO LOCALPLAYLIST (PLAYLISTNAME,MYDATECOLUMN) VALUES (\"%@\",getdate())
sqlite3_prepare_v2
后statement
指针为NULL
?
将sqlite3_prepare_v2
和sqlite3_step
的返回值与http://www.sqlite.org/c3ref/c_abort.html中的值进行比较,以便更好地了解出现问题的方法