2011-09-13 47 views
-1

我已經創建了一個數據庫。在我看來,我正在檢查提供的dat是否已經存在或不存在。如果存在則反彈是否則不。爲了這個,我有使用此代碼..如何比較數據庫中的數據?

+(BOOL)compareDataInDB:(NSString *)Start_text EndAddress:(NSString *)End_text Categories:(NSString *)Category_text{ 


BOOL flag=FALSE; 

if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) { 
    //int LastId="select Max(place_id) from Places"; 
    //  NSLog(@"%i",LastId); 
    const char *sql = "select * from Places where Start_text = Start_text and End_text = End_text and Category_text = Category_text"; 
    sqlite3_stmt *stmt; 
    int rtnVal = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); 
    if(rtnVal == SQLITE_OK) { 
     while (sqlite3_step(stmt) == SQLITE_ROW) { 
      flag=TRUE; 

     } 
    } 
    sqlite3_finalize(stmt); 
} 
sqlite3_close(db); 

if(flag==TRUE) 
    return YES; 
else 
    return NO; 

}

按鈕動作我都用這個代碼

-(IBAction)Find{ 
    BOOL tmp=[GlobalClass compareDataInDB:txtstart.text EndAddress:txtend.text Categories:txtCategory.text]; 
    if(tmp) 
    [GlobalClass storeDataInDB:txtstart.text EndAddress:txtend.text Categories:txtCategory.text]; 
    else 
     NSLog(@"exsit data in database"); 
} 

現在的問題是,它始終沒有返回。這段代碼中的錯誤是什麼?以及如何從數據庫中壓縮數據?

在此先感謝...

+2

如果(標誌== TRUE)返回YES?你在開玩笑嗎? –

+0

@米奇小麥這是什麼錯誤? – iRam11

+0

標誌僅爲TRUE或FALSE,那麼爲什麼不僅僅是返回標誌而不是比較它呢?這是「錯誤的」部分 –

回答

1

您必須對SQL語句使用以下代碼。

NSString *tempSQL = [[NSString alloc] initWithFormat:@"select * from Places where Start_text = '%@' and End_text = '%@' and Category_text = '%@'", Start_text, Start_text, Category_text]; 
const char *sql = [tempSQL cStringUsingEncoding:NSUTF8StringEncoding]; 
[tempSQL release]; 
0
+(BOOL)compareDataInDB:(NSString *)Start_text EndAddress:(NSString *)End_text Categories:NSString *)Category_text 
{  
    BOOL flag=NO; 

    if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) { 
     //int LastId="select Max(place_id) from Places"; 
     //  NSLog(@"%i",LastId); 
     const char *sql = "select * from Places where Start_text = Start_text and End_text = End_text and Category_text = Category_text"; 
     sqlite3_stmt *stmt; 
     int rtnVal = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); 
     if(rtnVal == SQLITE_OK) 
     { 
      sqlite3_bind_text(stmt, 1, [Start_text UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_text(stmt, 2, [End_text UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_text(stmt, 3, [Category_text UTF8String], -1, SQLITE_TRANSIENT); 

      while (sqlite3_step(stmt) == SQLITE_ROW) { 
       flag=YES; 
      } 
     } 
     sqlite3_finalize(stmt); 
    } 
    sqlite3_close(db); 

    return flag; 
    }