Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.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
Iphone 更新查询仅在模拟器中工作_Iphone_Database_Xcode_Sqlite - Fatal编程技术网

Iphone 更新查询仅在模拟器中工作

Iphone 更新查询仅在模拟器中工作,iphone,database,xcode,sqlite,Iphone,Database,Xcode,Sqlite,我正在使用firefox sqlite管理器帮助我构建iphone应用程序。其中我包含了一个更新查询,它在iPhone模拟器中非常有效。然而,当我从真正的机器(iphone)上运行时,它失败了。没有错误,它只是不更新数据库 我想到两个可能的原因: 1) 它更新的数据库不是模拟器中的数据库 2) 我读取的数据库不是已更新的数据库 有没有人有类似的经历 代码如下: NSFileManager *fileMgr = [NSFileManager defaultManager];

我正在使用firefox sqlite管理器帮助我构建iphone应用程序。其中我包含了一个更新查询,它在iPhone模拟器中非常有效。然而,当我从真正的机器(iphone)上运行时,它失败了。没有错误,它只是不更新数据库

我想到两个可能的原因: 1) 它更新的数据库不是模拟器中的数据库 2) 我读取的数据库不是已更新的数据库

有没有人有类似的经历

代码如下:

        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Exercises.sqlite"];

        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
        {
            NSLog(@"An error has occured.");
        }

        NSString *ranID = [@"UPDATE Status SET money = " stringByAppendingFormat:@"%d", money + 100];

        const char *sql2 = [ranID UTF8String];
        sqlite3_stmt *sqlStatement2;
        if(sqlite3_prepare(db, sql2, -1, &sqlStatement2, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement 2");
        }
        if (sqlite3_step(sqlStatement2)==SQLITE_ROW) 
            NSLog(@"succeed");

提前感谢。

发生的情况是,您试图在捆绑包中的数据库中写入,您没有对捆绑包中的文件的写入权限,如果要更新数据库,您需要将其复制到Documents目录

复制数据库文件的步骤

NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Exercises.sqlite"];

[[NSFileManager defaultManager] copyItemAtPath:dbPath toPath:documentsPath error:NULL];

然后始终使用新文件路径(
documentsPath
)访问数据库

应用程序包中的文件在设备上是只读的


您应该首先将其复制到应用程序沙箱中的某个位置,例如库/应用程序支持目录中。(您可以将-[NSFileManager URLsForDirectory:inDomains:]与NSApplicationSupportDirectory一起使用以查找此路径;请确保在尝试写入目录之前创建该目录。)

听起来您没有提交我们的更改。请参阅类似帖子,感谢您的快速回复。什么是文档目录?如何复制数据库?