1

我正在尝试为 SqlLite 数据库执行一个简单的硬编码插入语句。该代码有效,我从自己的 NSLog 中收到一条成功消息,但是,没有记录添加到数据库中。任何人都可以帮忙吗?谢谢!薇薇

-(void)addFavorites{
    const char *sqlInsert = "insert into rivers (stat_ID, stat_Name, state) values ('03186500','WILLIAMS RIVER','WA')";
    sqlite3_stmt *statement;

    sqlite3_prepare_v2(_database, sqlInsert, -1, &statement, NULL);
    if(sqlite3_step(statement) == SQLITE_DONE){
        NSLog(@"RECORD ADDED!");
    } else {
        NSLog(@"RECORD NOT ADDED!");
    }
    sqlite3_finalize(statement);
}
4

1 回答 1

0

您的应用程序委托中是否有这样的代码将数据库从包中复制到您的 NSDocuments 目录?确保将数据库复制到那里,然后在运行 sqlite3_open 时指向那里,而不是指向包。NSDocument 目录将在设备同步到 iTunes 或 iCloud 时保存,因此您希望数据库用于维护数据。

NSString *databaseName = @"MyDatabase.sqlite";  
NSArray *systemPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [systemPaths objectAtIndex:0];
NSString *databaseFullPath = [libraryDirectory stringByAppendingFormat:@"%@%@",@"/",databaseName];

//copy the database to the file system if it hasn't been done yet.
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL exists = [fileManager fileExistsAtPath:databaseFullPath];
if(exists == NO)
{
    NSString *dbPathInBundle = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:dbPathInBundle toPath:databaseFullPath error:nil];
}
于 2013-04-07T04:43:11.670 回答