将文本保存到sqlite3

时间:2012-09-24 13:24:16

标签: iphone objective-c

我想用下面的代码将一些文本保存到我的数据库中。但我无法让它工作......有用的是将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 个答案:

答案 0 :(得分: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文件并使用execute section执行查询。看看发生了什么:)

答案 1 :(得分:0)

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

答案 2 :(得分: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);

答案 3 :(得分:0)

通过将数据库移动到文档目录,确定了!谢谢你们......