Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite数据库未在iOS上保存_Ios_Sql_Database_Sqlite - Fatal编程技术网

SQLite数据库未在iOS上保存

SQLite数据库未在iOS上保存,ios,sql,database,sqlite,Ios,Sql,Database,Sqlite,我正在更新数据库中的一个条目,语句工作,代码在数据库中更新。一旦应用程序关闭并重新打开,尽管数据库尚未保存。它似乎创建了一个临时数据库,但实际上并没有保存到应用程序正在读取的数据库中 这是我的密码: -(void)updateDatabase:(int)Level andPlayer:(int)questionID{ DataClass *obj=[DataClass getInstance]; //NSFileManager *filemgr = [NSFileManager

我正在更新数据库中的一个条目,语句工作,代码在数据库中更新。一旦应用程序关闭并重新打开,尽管数据库尚未保存。它似乎创建了一个临时数据库,但实际上并没有保存到应用程序正在读取的数据库中

这是我的密码:

-(void)updateDatabase:(int)Level andPlayer:(int)questionID{
    DataClass *obj=[DataClass getInstance];
    //NSFileManager *filemgr = [NSFileManager defaultManager];
    NSString* Database =[NSString stringWithFormat:@"levelProgress.db"];
    NSString* databaseP = [[[NSBundle          mainBundle]resourcePath]stringByAppendingPathComponent:Database];
databasePath = [[NSString alloc]initWithString:databaseP];

const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;

if (sqlite3_open(dbpath, &questionDB) == SQLITE_OK) {
    NSString *querySQL = [NSString stringWithFormat:@"UPDATE levelProgress SET completed_questions=completed_questions+1 WHERE level=%d", obj.levelSelected];
    const char *query_stmt = [querySQL UTF8String];
    sqlite3_prepare_v2(questionDB, query_stmt, -1, &statement, NULL);
    if(sqlite3_step(statement)==SQLITE_DONE){
        NSLog(@"update worked");
    }else{
        NSLog(@"did not work");
    }
    sqlite3_finalize(statement);
    sqlite3_close(questionDB);
    }
}

非常感谢您提供的任何帮助。

将数据库文件
databaseP
从应用程序包复制到用户文件夹中,然后进行更新。您不能更新应用程序包中的任何文件(它们始终是只读的)


当数据库位于捆绑包中时,它是只读的。在写入数据库时,需要将数据库放在文档目录中


请查看此处接受的答案:

您无法更新只读文件。非常感谢,一旦文件位于用户文档目录中,用户是否可以看到我放在其中的文件?如果他们可以,你能阻止他们看到文件的内容吗?这个文件被复制到app sandbox的Documents目录中,而且相当安全。在非越狱手机上,除您的应用程序外,其他任何人都无法访问此文件。如果数据库不在库/应用程序支持中,请阅读此处的官方文档(特别是“应用程序沙盒”部分)。将其放在documents目录中将允许用户访问该文件。My Sqlite Db是一个可写文件,但对用户没有任何意义。如果在plist中启用“UIFileSharingEnabled”,这将允许用户在上面的路径中下载Sqlite数据库。将其存储在“NSLibraryDirectory”位置可以避免向用户公开数据库。
#define kDatabaseName (@"levelProgress.db")
- (void)checkAndCopyDatabaseIfNeeded
{
    if (!self.databasePath)
    {
        // Database should be present in user sandbox at root.
        self.databasePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], kDatabaseName, nil]];
    }

    // Check if the file already copied/exists.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success = [fileManager fileExistsAtPath:self.databasePath];

    if(!success)
    {
        // Copy the file from app bundle to user sandbox (Files in app bundle can not be edited).

        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kDatabaseName];

#if DEBUG
        BOOL isCopied =
#endif
        [fileManager copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil];

        NSAssert(isCopied, @"Problem copying database file to user document folder");
    }
}