1

此函数中的所有内容似乎都按预期工作,只是没有记录实际显示在数据库表中。如果我直接通过 Firefox sqlite manager 插件输入 sql,记录显示就好了。可能是一些令人沮丧的愚蠢,有什么想法吗?

- (IBAction)buttonInsertRecord:(id)sender
{
    // initializing db file access
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"KJV.sqlite"];

    // testing to see if database was found
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        // database not found
        labelStatus.text = @"database not found";
    }
    else
    {
        // database not found
        labelStatus.text = @"database found";
    }

    sqlite3_stmt *statement;
    sqlite3 *dbPointer;

    if (sqlite3_open(dbPath.UTF8String, &dbPointer) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO users (user_name, user_id) VALUES ('testuser', null);"];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(dbPointer, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            labelStatus.text = @"added";
        }
        else
        {
            labelStatus.text = @"failed";
        }

        sqlite3_finalize(statement);
        sqlite3_close(dbPointer);
    }
}
4

1 回答 1

4
    //your database is not writable because its in the bundle.
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"KJV.sqlite"];

如果你想要一个可读/可写的数据库,你可以这样做:

    - (void) createEditableCopyOfDatabaseIfNeeded 
    {   
        BOOL success;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"databasename.sqlite"];
        NSLog(@"writableDBPath == %@",writableDBPath);
        if ([fileManager fileExistsAtPath:writableDBPath])
        {
            NSLog(@"Database already exist");
        }
        else 
        {
            NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"databasename.sqlite"];
            success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
            if (!success) 
                NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    }

以及如何添加数据

- (void) addToDatabase
{
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"databasename.sqlite"];

        if(sqlite3_open([databasePath UTF8String], &mDatabase_update) == SQLITE_OK)
        {

            NSString *sqlQuery = [NSString stringWithFormat: @"Insert into table 
             .
             .
             .
             .
于 2012-12-21T01:00:30.127 回答