hum*_*rim 0 sqlite iphone ipad ios
我的问题出现在设备中,但不出现在模拟器中.
nDBres=sqlite3_prepare_v2(databaseConn, sqlStatement, -1, &compiledStatement,NULL)
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试运行的插入查询.在模拟器中它返回0,而在设备中它返回8.此后每当我尝试运行任何其他写操作时,应用程序崩溃.
我为此疯狂.
您可以参考sqlite3错误代码列表作为sqlite3 API文档(http://www.sqlite.org/c3ref/c_abort.html)的一部分.错误代码8是SQLITE_READONLY
("尝试编写只读数据库").
您可能知道,iOS沙盒应用程序上运行的应用程序,因此您必须确保在操作系统公开的用于创建应用程序可写文件的区域之一中创建数据库.
关于如何在iOS上设置sqlite3项目有一个不错的教程http://icodeblog.com/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/.
从该教程中,您的问题最重要的部分可能createEditableCopyOfDatabaseIfNeeded
是应用程序委托中的方法.这说明了如何在第一次启动应用程序时确保创建可编辑的数据库文件:
(注意,这不是我的代码......我正在从icodeblog.com上的教程中复制它,在那里他们详细解释了它)
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self createEditableCopyOfDatabaseIfNeeded];
[self initializeDatabase];
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"todo.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"todo.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2136 次 |
最近记录: |