【问题标题】:SQLITE : why I can't write on device (iphone) but I can write on simulator? [duplicate]SQLITE:为什么我不能在设备(iphone)上写,但我可以在模拟器上写? [复制]
【发布时间】:2013-01-18 21:13:58
【问题描述】:

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

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

有没有设置权限之类的配置?

PS:当我更新或插入 sqlite(从设备)时没有错误。

这是我的代码(打开 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);
        }

}

【问题讨论】:

  • 您不能从主包中写入 sqlite 文件,因为它们是只读的。您必须将 sqlite 文件从主包复制到 Documents 目录,然后使用复制到文档目录中的 sqlite 文件。希望这个帮助..
  • 这个链接可以帮助你复制gist.github.com/1071312

标签: iphone objective-c xcode sqlite


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    你可以在任何地方创建你的 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);
    }
    

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

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 2021-07-27
      相关资源
      最近更新 更多