2011-09-05 81 views
4

我想從我的數據庫中的數據,但我沒有得到解決掉 我嘗試這樣迷糊中sqlite的select語句

- (空)信息

{ 
    //[self createEditableCopyOfDatabaseIfNeeded]; 
    NSString *filePath = [self getWritableDBPath]; 

    sqlite3 *database; 
    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) 
    { 
     NSString *qu = @"Select Name FROM empl where SyncStatus ='1'"; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, [qu UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 
      char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0); 

      if (firstColumn==nil) 
      { 
       NSString *name= [[NSString stringWithUTF8String:firstColumn]autorelease]; 
       NSLog(@"%c",name); 

      } 
      if(sqlite3_step(compiledStatement) != SQLITE_ROW) 
      { 

       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"User is not valid" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [alert show]; 
       [alert release]; 
       alert = nil; 
      } 
     } 
     sqlite3_finalize(compiledStatement); 
    } 
    sqlite3_close(database); 
} 

得到它崩潰, 這讓我錯誤的原因: '* + [NSString的stringWithUTF8String:]:NULL CSTRING' 在第一擲 *調用堆棧:

回答

3

這樣使用

if(sqlite3_step(compiledStatement) == SQLITE_ROW) 
      { 
       char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0); 
       NSString *name= [[NSString alloc]initWithUTF8String:firstColumn]; 


      } 

我希望這將有助於你試試這個

+0

謝謝它幫助我&amresh也說我一樣 – Harish

3

我覺得firstColumn是空的,我使程序崩潰。您嘗試檢查數據庫中的數據爲空。

+0

我的數據庫是不是null我火選擇JourneyName FROM UserJourney其中SYNCSTATUS =「1」的sqlite的瀏覽器,它給我的數據 – Harish

3

這實際上是顯而易見的,因爲你正在試圖獲取這是NULL 嘗試此代碼對你的代碼檢查NULL數據:

if (sqlite3_column_text(compiledStatement, 0)!= nil) 

if (sqlite3_column_text(compiledStatement, 0)!= nil) 
    char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0); 
else 
    char *firstColumn = @""; 

NSString *name= [[NSString stringWithUTF8String:firstColumn]autorelease]; 
NSLog(@"%c",name); 

試試這個... 希望它有幫助....

+0

你的回答也接近了我的問題,但感謝你的幫助 – Harish

0

我認爲有2個問題與這段代碼。

  • 正如早已指出你檢查等於零而不是 從零不同。
  • 要訂購的SQL函數應該是:

    1. sqlite3_open
    2. sqlite3_prepare_v2
    3. sqlite3_step
    4. sqlite3_column_text
您也可以瞭解在這個命令的語句: http://www.iosdevelopment.be/sqlite-tutorial/