SQLite3数据库实际上并没有插入数据 - iPhone [英] SQLite3 database doesn't actually insert data - iPhone

查看:565
本文介绍了SQLite3数据库实际上并没有插入数据 - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的数据库中添加一个新条目,但它无效。没有抛出错误,并且应该在插入运行后执行的代码,这意味着查询没有错误。但是,数据库中没有添加任何内容。我已经尝试了两个预处理语句和更简单的sqlite3_exec,结果相同。

I'm trying to add a new entry into my database, but it's not working. There are no errors thrown, and the code that is supposed to be executed after the insertion runs, meaning there are no errors with the query. But still, nothing is added to the database. I've tried both prepared statements and the simpler sqlite3_exec and it's the same result.

我知道我的数据库正在加载,因为tableview的信息(以及后续的tableviews)从数据库加载。连接不是问题。

I know my database is being loaded because the info for the tableview (and subsequent tableviews) are loaded from the database. The connection isn't the problem.

此外,sqlite3_last_insert_rowid(db)的日志返回下一行的正确数字。但是,信息仍未保存。

Also, the log of the sqlite3_last_insert_rowid(db) returns the correct number for the next row. But still, the information is not saved.

这是我的代码:

db = [Database openDatabase];           
NSString *query = [NSString stringWithFormat:@"INSERT INTO lists (name) VALUES('%@')", newField.text];
NSLog(@"Query: %@",query);

sqlite3_stmt *statement;

if (sqlite3_prepare_v2(db, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
  if (sqlite3_step(statement) == SQLITE_DONE){
    NSLog(@"You created a new list!");
    int newListId = sqlite3_last_insert_rowid(db);
    MyList *newList = [[MyList alloc] initWithName:newField.text idNumber:[NSNumber numberWithInt:newListId]];
    [self.listArray addObject:newList];
    [newList release];
    [self.tableView reloadData];
    sqlite3_finalize(statement);
  }
  else {
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(db));
  }
}
[Database closeDatabase:db];

同样,没有抛出任何错误。 prepare和step语句分别返回SQLITE_OK和SQLITE_DONE,但没有任何反应。

Again, no errors have been thrown. The prepare and step statements return SQLITE_OK and SQLITE_DONE respectively, yet nothing happens.

感谢任何帮助!

推荐答案

确保已将数据库移动到可写目录,例如 NSDocumentDirectory NSLibraryDirectory

Make sure you have moved the database to writable directory such as NSDocumentDirectory and NSLibraryDirectory.

您在XCode中看到的数据库文件位于 MainBunble 中,该文件可读但不可写。

The database file you see in XCode resides in the MainBunble which is readable but not writable.

如果数据库存在于您决定的(可写)位置,则需要添加一个在运行时检查的方法,如果它不存在,请使用 NSFileManager 将数据库文件复制到可写目录路径。

You need to add a method to check at runtime if the database exists in your decided (writable) location, if it doesn't exist, use NSFileManager to copy the database file to the writable directory path.

这是我的应用程序Snap-it中的一个片段:

Here's a snippet from my app Snap-it Notes:

+ (BOOL)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    BOOL newInstallation;
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"database_file_path"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    newInstallation = !success;
    if (success) {
        [fileManager release];
        return newInstallation;
    }
    // The writable database does not exist, so copy the default to the appropriate location.
    //NSLog(@"database does not exist");
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database_file_path"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    [fileManager release];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
    return newInstallation;
}

这篇关于SQLite3数据库实际上并没有插入数据 - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆