为什么我不能在设备

为什么我不能在设备

本文介绍了SQLITE:为什么我不能在设备(iphone)上写但我可以在模拟器上写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我只能在iphone上读取我的Sqlite数据库,却不能写任何东西...在模拟器上一切正常..

I wonder why I only can read my Sqlite database on iphone but can't write anything...everything work just fine on the simulator..

是否有任何设置权限的配置?

is there any configuration to set the permission or something??

PS:从设备更新或插入sqlite时没有错误.

PS: there is no error while I'm updating or inserting to sqlite (from device).

这是我的代码(打开sqlite):

here's my code (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);
        }

}

推荐答案

您不能在主捆绑包中书写.您可以将数据保存到文档目录
您必须将数据库复制到文档目录,然后必须对文档目录中的数据库文件执行操作.

You can't write in your main bundle. You can save data to documents directory
You have to copy your data base to document directory and then have to perform operations on the data base file which is in document directory.

将此代码放入应用程序委托中以复制您的数据库

Put this code in app delegate to copy your 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.");
        }
}

和初始化方法

- (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;
}

这篇关于SQLITE:为什么我不能在设备(iphone)上写但我可以在模拟器上写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:08