2012-09-24 68 views
0

我想使用下面的代碼將一些文本保存到我的數據庫中。但我不能得到它的工作... Useally那就是SMT存儲到數據庫的方式......將文本保存到sqlite3

-(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); 
} 
} 

但由於某些原因,只在模擬器中工作!但在我的手機也不需額外的工作......它總是進入人在這裏:

if (sqlite3_step(sqlStatement) == SQLITE_DONE) 

回答

1

我無法找到問題,但我有一些suggesions,

不知道這是你的主要問題,,但它看起來像你正在修改數據庫,這太糟糕了。例如,發佈更新時,該捆綁包將被擦除/替換。

當您啓動應用程序時,您應該在文檔目錄中查找數據庫,如果它不在第一次啓動時,您將默認數據庫從包中複製到文檔中目錄。

代碼:

- (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」是設備不同的文件,但對於simulator.So相同的文件請您查詢或代碼串的情況。

此外,您還可以在控制檯中記錄insertSQL,將其從控制檯複製並打開Bundle sqlite文件並使用execute部分執行查詢。看看發生了什麼:)

+0

Ye我的數據庫是正確的捆綁!所以我必須將它移動到文檔?!然後參考我的項目? – davidOhara

0

主包在設備上是隻讀的。嘗試將數據庫文件移動到文檔目錄或其他可以寫入的地方。

+0

把你的意思轉移到文檔上,意味着什麼? – davidOhara

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); 
0

確定由數據庫移動到文件目錄得到它!謝謝你們...