0

我想使用以下代码将一些文本保存到我的数据库中。但我无法让它工作......通常这是将 smt 存储到数据库中的方式......

-(void)saveToDB
{
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"datenbankSpeed"];
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: %s", sqlite3_errmsg(db));

}


if (sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)
{

    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO DATEN(typ, date, download, upload, ping, comment) VALUES (\"%@\", \"%@\", \"%@\",\"%@\", \"%@\",\"%@\")", @"1", @"24.09.2012", topDownloadLabel.text, topUploadLabel.text, pingLabel.text, @"Comment"];

    const char *insert_stmt = [insertSQL UTF8String];
    printf("%s\n", insert_stmt);
    NSLog(@"%@", [NSString stringWithUTF8String:insert_stmt]);

    if (sqlite3_prepare_v2(db, insert_stmt, -1, &sqlStatement, NULL) == SQLITE_OK)
    {
        if (sqlite3_step(sqlStatement) == SQLITE_DONE)
        {
            UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"All good"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
            [didFailWithErrorMessage show];
            [didFailWithErrorMessage release];
        }
        else
        {
            UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"nada"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
            [didFailWithErrorMessage show];
            [didFailWithErrorMessage release];
        }

    }
            sqlite3_finalize(sqlStatement);
    sqlite3_close(db);
}
}

但由于某种原因,它只适用于模拟器!但在我的手机上它不起作用......它总是在这里进入 ELSE:

if (sqlite3_step(sqlStatement) == SQLITE_DONE)
4

4 回答 4

1

我无法找到问题,但是我有一些建议,

不确定这是否是您的主要问题,但看起来您正在修改您的数据库,这太糟糕了。例如,当您发布更新时,捆绑包将被删除/替换。

当您的应用程序启动时,您应该在文档目录中查找数据库,如果它不存在(这不是您第一次启动),您将默认数据库从捆绑包复制到 docs 目录。

代码:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    [self copyDatabaseIfNeeded];

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

- (void) copyDatabaseIfNeeded {

    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@\"datenbankSpeed.sqlite\"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
        NSLog(@\"Database file copied from bundle to %@\", dbPath);

        if (!success) 
            NSAssert1(0, @\"Failed to create writable database file with message '%@'.\", [error localizedDescription]);
    } else {

        NSLog(@\"Database file found at path %@\", dbPath);

    }
}

设备区分大小写iPhone设备关心字符串的大小写

例如“database.sqlite”和“DataBase.sqlite”对于设备来说是不同的文件,但对于模拟器来说是相同的文件。所以请检查你的查询或代码字符串情况。

您还可以在控制台中记录 insertSQL,从控制台复制它并打开 Bundle sqlite 文件并使用执行部分执行查询。看看发生了什么:)

于 2012-09-24T13:33:24.273 回答
0

主包在设备上是只读的。尝试将您的数据库文件移动到文档目录或您可以写入的其他位置。

于 2012-09-24T13:33:57.890 回答
0

试试这样

  sqlite3_stmt *stmt;

            int x;
            char *update = "insert into meetingPhoneAlarm values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
            x = sqlite3_prepare_v2(database, update, -1, &stmt, nil);
            NSLog(@"x=%d",x);


            if (x == SQLITE_OK) 
            { 
                sqlite3_bind_text(stmt, 1, NULL,-1, NULL);  
                sqlite3_bind_text(stmt, 2, [txt_subject.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 3, [txt_location.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 4, [txt_meetwith.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 5, [btn_partyDate.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 6, [btn_partyTime.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 7, [txt_notes.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 8, [btn_snooze.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 9, [string_ascending UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 10, [string_volume UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 11, [string_alarmsound UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 12,[btn_alarmDate.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 13,[btn_AMPM.titleLabel.text UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 14,[string_vibration UTF8String],-1, NULL);
                sqlite3_bind_text(stmt, 15,[string_repeatOption UTF8String],-1, NULL);

            }       
            if (sqlite3_step(stmt) != SQLITE_DONE){}
            //  NSLog(@"Error: %@",errorMsg); 
            sqlite3_finalize(stmt);
于 2012-09-24T13:54:59.807 回答
0

好的,将数据库移动到文档目录就可以了!多谢你们...

于 2012-09-25T09:50:59.490 回答