0

我花了几天时间试图修复这段代码,但我放弃了!!如果他按下“保存”按钮,我正在做的是从用户那里获取输入并将其插入数据库。当我运行此代码并按下“保存”按钮后。1-我无法退出视图(到另一个选项卡)。2-插入未应用!

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];
    sqlite3 *database;


    //Copy the database from the package to the usrrs filesystem
    if (sqlite3_open([dbPath UTF8String], &database)== SQLITE_OK) {//start if 
        NSLog(@"dataBaseOpen");
        const char *sqlStatement = "insert into Location (Latitude,Longitude,LocationName,Tag) VALUES (?,?,?,?)";
        // 
        sqlite3_stmt *compileStatement;

        if (sqlite3_prepare_v2(database, sqlStatement, -1, &compileStatement, NULL) == SQLITE_OK) { // start if 2
            if(SQLITE_DONE!=sqlite3_step(compileStatement))
            {
            sqlite3_bind_double(compileStatement, 1, [latitudeTextField.text doubleValue]);

            sqlite3_bind_double(compileStatement, 2, [longitudeTextField.text doubleValue]);

            sqlite3_bind_text( compileStatement, 3, [lName.text UTF8String], -1, SQLITE_TRANSIENT);

            sqlite3_bind_text(compileStatement, 4, [myText.text UTF8String] ,-1, SQLITE_TRANSIENT);
             NSLog( @" successfully done" );
            if(sqlite3_step(compileStatement) != SQLITE_DONE ) {
                NSLog( @" not done" );
            }  
            }

        }
    }

我在文件管理器中放入了 appDelegate 代码来应对:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0];

NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];


NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL success = [fileManager fileExistsAtPath:dbPath];

if (success) {

    NSLog(@"we have the database");

} else {

    NSLog(@"we have no database");

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CAMAClientDB2.sqlite"];


    BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];

    if (moved) {
        NSLog(@"database copied");
    }



}

我错过了什么 !!问题是什么 !!

4

1 回答 1

0

对于您的#2 问题: 1. 您有一个额外的 sqlite_step 调用。你的顺序应该是:

    sqlite3_prepare_v2()
            sqlite_bind_....
            sqlite_bind_....
            for as many bind's as you need, and then
            sqlite_step()
  1. 如果 sqlite_step 仍然失败,请检查返回代码并在文档中查找指导

  2. 如果您的 MacOS 应用程序是沙盒的,这将不起作用。目前无法自行更新 sqlite 数据库。您必须获得用户对 sqlite 文件所在目录的写入权限,或者必须将其放入包中(这实际上是同一件事)

于 2012-11-09T21:05:34.850 回答