2012-01-27 58 views
1

我想將3段數據添加到SQLite3數據庫。在教程的幫助下,我快到了。以下是我的代碼。從Xcode內向SQLite3數據庫添加數據

我收到了我期望從NSLogs得到的結果。我發現數據庫沒有問題,我編譯聲明發送到數據庫沒有問題,但當應用程序到達sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK我期待數據被添加到DB,但它不是,我也期待顯示一個UIAlertView以告訴用戶ERROR OS SUCCESS。我希望你能看到我沒有的東西。

- (IBAction)addData:(id)sender { 
    NSString *n = [[NSString alloc] initWithString:[name text]]; 
    NSString *a = [[NSString alloc] initWithString:[age text]]; 
    NSString *c = [[NSString alloc] initWithString:[color text]]; 
    NSLog(@"%@ - %@ - %@",n,a,c); 
    [self insertDataName:n Age:a Color:c]; 
} 

這個NSLog如預期的那樣返回文本屬性。

- (IBAction)hide:(id)sender { 
    [name resignFirstResponder]; 
    [age resignFirstResponder]; 
    [color resignFirstResponder]; 

} 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    DBName = @"mydatabase.sqlite"; 
    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentsPaths objectAtIndex:0]; 
    DBPath = [documentsDir stringByAppendingPathComponent:DBName]; 

} 


-(void)checkAndCreateDB { 
    BOOL Success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    Success = [fileManager fileExistsAtPath:DBPath]; 

    if(Success) { 
     NSLog(@"DB Found");  
     return; 
    } 

    NSLog(@"DB Not Found"); 
    NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName]; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; 

} 

NSLog回報

-(void)insertData Name:(NSString*)n Age:(NSString*)a Color:(NSString*)c { 
    [self checkAndCreateDB]; 
    sqlite3 *database; 
    if (sqlite3_open([DBPath UTF8String],&database)==SQLITE_OK) { 
     NSString *statement; 
     sqlite3_stmt *compliedstatement; 
     statement = [[NSString alloc] initWithFormat:@"insert into table1 values ('%@', '%@', '%@')",n,a,c]; 
     const char *sqlstatement = [statement UTF8String]; 
     NSLog(@"%@",statement); 

     if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) { 

     if (SQLITE_DONE!=sqlite3_step(compliedstatement)) { 
      NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database)); 
      UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  

      [AlertOK show]; 

     } 
     else { 
      UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];    
      [AlertOK show]; 
      } 
     } 
     sqlite3_finalize(compliedstatement); 

    } 
    sqlite3_close(database); 

} 

而最後NSLog上面顯示insert into table1 values ('n', 'a', 'c')爲我所期望的。

插入時出現錯誤,我無法弄清楚。

僅供參考數據庫是使用SQLiteManager創建的。 SS下面:

SQLiteManager table1 SS

+0

你能更具體的到底發生了什麼?你的陳述已經準備好,但是你面對的是什麼樣的錯誤?這是(SQLITE_DONE!= sqlite3_step(compliedstatement))執行?那麼發生了什麼? – Byte 2012-01-27 20:28:47

+0

我得到了'UIAlertView'成功,但是當我在SQLiteManager中打開我的數據庫並從'table1'中選擇*時,有0個條目。 – 2012-01-28 00:08:36

回答

1

好的,您是工作在模擬器或者的iDevice?在iDevice中,您無法創建外部SQL數據庫(至少據我所知)。你必須動態創建它,因爲沙盒。 「僅供參考,數據庫是使用SQLiteManager創建的。」檢查一下。

如果這不是問題嘗試從 「準備......」 改變你的代碼,以 「執行」

從...

if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) { 

    if (SQLITE_DONE!=sqlite3_step(compliedstatement)) { 
     NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database)); 
     UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  

     [AlertOK show]; 

    } 
    else { 
     UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];    
     [AlertOK show]; 
     } 
    } 
    sqlite3_finalize(compliedstatement); 

要...

sqlite3_exec(database, [sqlstatement UTF8String], NULL, NULL, &error); 

做一個登錄「錯誤」,因爲這是非常重要的。

如果你想彈出一個UIAlert做到這一點

if (error !=nil) 
    //show alert for error 
else 
    //show alert for success 
+0

偉大的回答,這指出我在正確的方向。謝謝! – 2012-01-30 19:12:33