2012-03-13 83 views
0

我有一個表格,它有兩列作爲組合(文本),標誌(數字)。我試圖檢索標誌的值取決於組合值使用選擇查詢中的where子句。從數據庫中提取特定數據時遇到困難

問題是,當組合值從1開始時,例如,在組合='1001'的情況下,它從表中檢索標誌的正確值,但是當它從0開始時例如如果組合='0010',則不會從數據庫中檢索到任何內容。我調試了代碼,但沒有得到原因。以下是數據庫映像。

This is table having two columns.

-(void)fetchfromDatabase 
{ 
     sqlite3 *database; 

NSString *strPath = [[NSBundle mainBundle]pathForResource:@"ken" ofType:@"db"]; 
NSLog(@"DBPath : %@",strPath); 

const char *dbPath = [strPath UTF8String]; 

if(sqlite3_open(dbPath, &database)==SQLITE_OK) 
{ 
    NSLog(@"Database is Ok"); 

    // const char *cConcate = [concate UTF8String]; 
    NSString *query = [NSString stringWithFormat:@"select * from kentable where combination =%@",concate]; 
    NSLog(@"final concate in database:%@",concate); 
    NSLog(@"Query:%@",query); 
    const char *sqlQuery =[query cStringUsingEncoding:NSASCIIStringEncoding]; 
    NSLog(@"char query %s", sqlQuery); 
    sqlite3_stmt *queryStatement; 

    if(sqlite3_prepare_v2(database, sqlQuery,-1,&queryStatement, NULL)==SQLITE_OK) 
    { 
     NSLog(@"Conversion is Ok"); 


     while (sqlite3_step(queryStatement)==SQLITE_ROW) 
     { 
      NSLog(@"in While Statement of row"); 
      int flag=sqlite3_column_int(queryStatement, 1); 

      NSLog(@"question 5:%d",flag); 
     } 
     sqlite3_reset(queryStatement); 
    } 
    else 
    { 
     NSLog(@"ERROR"); 
    } 

    sqlite3_close(database); 

} 

}

+1

我想你可能想在你的查詢中加引號。儘管我更喜歡使用CoreData的sqlite。 – govi 2012-03-13 11:07:12

+0

使用核心數據!速度更快,您將擁有更少的代碼進行管理。你不會寫選擇語句 – 2012-03-13 11:08:26

+0

@govi你是對的。非常感謝。它運行良好。 – 2012-03-14 10:12:10

回答

1

我也有這樣的任務..它運作良好...只是通過

的NSString更改代碼*數據= [自我狀態:組合];

-(NSString *) status:(NSString *)concate 
    { 
    databasePath = [[NSString alloc] initWithString: [[NSBundle mainBundle] pathForResource:@"ken" ofType:@"db"]]; 
     sqlite3_stmt *statement; 
     NSString *aName; 
     const char *dbpath = [databasePath UTF8String]; 

     if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
     { 
      NSString *querySQL = [NSString stringWithFormat: 
            @"SELECT combination, flag FROM details WHERE combination =\"%@\"", 
            concate]; 

      const char *query_stmt = [querySQL UTF8String]; 
      sqlite3_prepare_v2(database, 
           query_stmt, -1, &statement, NULL); 
      if (sqlite3_step(statement) == SQLITE_ROW) 
      { 
       // Read the data from the result row 
       aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]; 

       NSLog(@"same %@" ,aName) ; 

      } 
      else 
      { 
       aName=nil; 
      } 

    sqlite3_finalize(statement); 
      sqlite3_close(database); 

     } 


[databasePath release]; 
    return aName; 
} 
3

嘗試更改代碼如下。不要介意,但是如你所做的那樣準備sql查詢更好地遵循標準慣例。你可以使用sqlite3_bind_text函數來綁定字符串。

if(sqlite3_open(dbPath, &database)==SQLITE_OK) 
{ 
    const char *sqlQuery = "select * from kentable where combination = ?"; 
    sqlite3_stmt *queryStatement; 

    if(sqlite3_prepare_v2(database, sqlQuery,-1,&queryStatement, NULL)==SQLITE_OK) 
    { 
     NSLog(@"Conversion is Ok"); 
     sqlite3_bind_text(insertStmt,1, [concate UTF8String], -1, SQLITE_TRANSIENT);  

     while (sqlite3_step(queryStatement)==SQLITE_ROW) 
     { 
      NSLog(@"in While Statement of row"); 
      int flag=sqlite3_column_int(queryStatement, 1); 

      NSLog(@"question 5:%d",flag); 
     } 
     sqlite3_reset(queryStatement); 
    } 
    else 
    { 
     NSLog(@"ERROR"); 
    } 
    sqlite3_close(database); 
} 
+0

這是否幫助你? – 2012-03-15 12:52:01

相關問題