SQLITE:为什么我不能在设备(iphone)上写,但我可以在模拟器上写?

时间:2013-02-04 07:33:22

标签: iphone objective-c xcode sqlite

  

可能重复:
  How should I specify the path for an sqlite db in an iPhone project?

我想知道为什么我只能在iphone上读取我的Sqlite数据库但却无法写任何东西...... 一切都在模拟器上正常工作..

是否有任何配置来设置权限或什么?

PS:我正在更新或插入sqlite(来自设备)时没有错误。

这是我的代码(open sqlite):

- (id)init {
    if ((self = [super init])) {
        NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"test"
                                                             ofType:@"sqlite"];

        if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
            NSLog(@"Failed to open database!");
        }
    }
    return self;
}

@end

和我的“更新”代码:

-(void)addCoin:(int)score
{
    NSString *query = @"select coin_user from user where id_user = '1'";
    //query = @"SELECT id_soal FROM soal where jawaban_soal = 'romin'";

    sqlite3_stmt *statement;
    int total;
    if (sqlite3_prepare(_database, [query UTF8String], -1, &statement, nil)
        == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            total = sqlite3_column_int(statement, 0);
        }
        sqlite3_finalize(statement);
    }

    query = [NSString stringWithFormat:@"update user set coin_user = '%i' where id_user = '1'",total + score];

    if (sqlite3_prepare(_database, [query UTF8String], -1, &statement, nil)
        == SQLITE_OK) {
        sqlite3_step(statement);
        sqlite3_finalize(statement);
        }

}

2 个答案:

答案 0 :(得分:3)

您无法在主套装中书写。您可以将数据保存到文档目录
您必须将数据库复制到文档目录,然后必须对文档目录中的数据库文件执行操作。

将此代码放入app delegate中以复制db

NSString* docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* dbPath = [docPath stringByAppendingPathComponent:@"test.sqlite"];
NSFileManager *fm = [NSFileManager defaultManager];

// Check if the database is existed.
if(![fm fileExistsAtPath:dbPath])
{
    // If database is not existed, copy from the database template in the bundle
    NSString* dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"sqlite"];
    NSError* error = nil;
    [fm copyItemAtPath:dbTemplatePath toPath:dbPath error:&error];  
        if(error){
            NSLog(@"can't copy db.");
        }
}

和init方法

- (id)init {
    if ((self = [super init])) {
            NSString* docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString* dbPath = [docPath stringByAppendingPathComponent:@"test.sqlite"];

           if (sqlite3_open([dbPath UTF8String], &_database) != SQLITE_OK) {
               NSLog(@"Failed to open database!");
           }
    }
    return self;
}

答案 1 :(得分:-1)

您可以在任何地方创建sqlite数据库并提供数据库路径。这对你来说很容易。请查看以下代码

{
        sqlite3_stmt *statement;

        NSString *lmk= @"Volumes/Outbox/test.sqlite";
        const char *dbpath=[lmk UTF8String];

        if (sqlite3_open(dbpath, &contactDB3)==SQLITE_OK)
        {
            NSLog(@"Database is connected sucessfully");
        }
            else
                NSLog(@"Not connected");
            sqlite3_close(contactDB3);
}

现在您可以轻松选择插入更新删除。