这是我在Appdelegate.m
已经使用复制数据库总是失败,如果(sqlite3_step(声明)== SQLITE_DONE)
- (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)
{
[email protected]"Playlist Created";
}
else
{
[email protected]"Error occured";
}
return status;
}
}
我的问题是这样的prepare_v2始终没有变TDONE。它总是执行else部分。
if (sqlite3_step(statement) == SQLITE_DONE)
这是什么问题?请帮我
经常检查sqlite3_prepare_v2'的'返回值一个更好的了解。如果失败,请记录'sqlite3_errmsg()'的结果以查看问题。切勿使用字符串格式来创建查询。改为正确使用'sqlite3_bind_xxx'。 – rmaddy
什么是getdate()函数? SQLite中没有这样的东西。 – rmaddy
顺便说一句,虽然我很高兴你解决了你的问题,但使用'stringWithFormat'将值插入SQL语句是不可取的。你的SQL可能应该像'INSERT INTO LOCALPLAYLIST(PLAYLISTNAME,MYDATECOLUMN)VALUES(?,datetime())',然后使用'sqlite3_bind_text(语句,1,[PlaylistName UTF8String],-1,NULL)'绑定值到'?'占位符。如果播放列表名称中包含双引号,则现有代码将失败。 – Rob