2012-01-04 174 views
2

我想插入數據到sqlite..hhile插入即時通訊獲得重複在我的代碼..我看到與它有關的問題和即時通訊瞭解有關..所有隻是幫助我這個代碼來檢查數據插入到數據庫之前存在...檢查數據是否存在之前插入到iPhone的SQLite數據庫

- (IBAction)AddtoFavourites:(id)sender { 
    sqlite3 *database; 
    [email protected]"dataTable.sqlite"; 
    NSArray *documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentdir=[documentpath objectAtIndex:0]; 
    dbPath=[documentdir stringByAppendingPathComponent:dbName]; 
    if(sqlite3_open([dbPath UTF8String], &database)==SQLITE_OK){ 
//I want to check data it exists already into database.... 
     NSString *sqlStatement=[NSString stringWithFormat:@"insert into Persons(PersonName,CompanyName,ImgUrl)values(\"%@\",\"%@\",\"%@\")",model.personName,model.companyName,model.imgurl]; 
      const char *insertSQL=[sqlStatement UTF8String]; 
     char *errMsg; 
     sqlite3_exec(database, insertSQL, NULL,NULL,&errMsg); 
     NSLog(@"Add to Favourites");   
    } 
} 

回答

3

首先使用select查詢任何列的爲:

const char *sqlStatement = "SELECT PersonName FROM Persons"; 

我猜你會分配到的SQLStatement編制陳述

  while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
      // Read the data from the result row 
      NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 


      [dataArray addObject:aName]; 
      NSLog(@"%@",aName); 
} 

將檢查這個

+0

我已經做了這一切對我的appdelegate讀取數據庫... – 2012-01-04 07:12:04

+0

如果你的新PERSONNAME等於aName那麼不插入 – 2012-01-04 07:19:44

+2

我找到了答案..感謝你..我使用UNIQUE for personName在我的sqlite數據庫中,並使用插入或忽略查詢語句,而不是查詢語句... – 2012-01-04 07:21:31

相關問題