2012-12-20 75 views
0

在我的視圖控制器中它顯示充分的細節。所以當用戶點擊添加按鈕時,它會將數據插入到sqlite中。問題是如何檢查數據是否被插入。ios sqlite插入數據如果沒有插入

//this code is in viewDidLoad 
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM exhibitor"]; 
    sqlite3_stmt *statement; 
    check= 0; 
    if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK){ 
     while (sqlite3_step(statement)==SQLITE_ROW) { 
      char *exName2 = (char *) sqlite3_column_text(statement, 1); 
      NSString *exNameStr = [[NSString alloc] initWithUTF8String:exName2]; 
      NSString *exhibName = [exhibitionArticle objectForKey:@"ex_name"]; 

      if([exNameStr isEqualToString:(exhibName)]){ 
       check += 1;  
      }else{ 
       check += 0; 
      } 

     } 
     NSLog(@"%d Result is", check); 
    }  //this code checking my data is in sqlite or not 

如果數據不在源碼然後按鈕插入的數據。

- (IBAction)addExhibitor:(id)sender { 

    NSLog(@"%d this is addExhibitor int", check); 
    if(check == 0){ 
    NSDate *exhibAddDate = [NSDate date]; 
    NSString *exhibName = [exhibitionArticle objectForKey:@"ex_name"]; 
    NSString *exhibAbout = [exhibitionArticle objectForKey:@"ex_about"]; 
    NSString *exhibBooth = [exhibitionArticle objectForKey:@"ex_booth"]; 
    NSString *exhibAddress = [exhibitionArticle objectForKey:@"ex_address"]; 
    NSString *exhibTelephone = [exhibitionArticle objectForKey:@"ex_telephone"]; 
    NSString *exhibFax = [exhibitionArticle objectForKey:@"ex_fax"]; 
    NSString *exhibWebSite = [exhibitionArticle objectForKey:@"ex_website"]; 
    NSString *exhibEmail = [exhibitionArticle objectForKey:@"ex_email"]; 

    NSString *sql1 = [NSString stringWithFormat:@"INSERT INTO exhibitor ('exhibAddDate', 'exhibName', 'exhibAbout', 'exhibBooth', 'exhibAddress', 'exhibTelephone', 'exhibFax', 'exhibWebSite', 'exhibEmail') VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", exhibAddDate, exhibName, exhibAbout, exhibBooth, exhibAddress, exhibTelephone, exhibFax, exhibWebSite, exhibEmail ]; 

    char *err; 
    if(sqlite3_exec(db, [sql1 UTF8String], NULL, NULL, &err) != SQLITE_OK){ 

     sqlite3_close(db); 
     NSAssert(0, @"Could not update table"); 
    }else{ 

     NSLog(@"Table Updated"); 
    } 
    } 
    else{ 
    // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Same Data" message:@"Your data already there" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     // [alert show]; 

    } 


} 
+0

你爲什麼不只是做一個'SELECT COUNT(*)FROM WHERE參展商....'爲參展商(有點像你在第一個片段證明,但是這一次,加一個'WHERE'子句),如果你得到一個非零值,你知道它在那裏? – Rob

+0

不相關的,而不是用'stringWithFormat'構建你的SQL,你應該使用'?'佔位符,然後執行'sqlite3_bind _...'命令。如果參展商的名字是「Joe's Bar and Grill」,那麼你的代碼會發生什麼?更糟糕的是,你的代碼可能會受到SQL注入攻擊,如果你使用'sqlite3_bind _...'命令,就會阻止它。 – Rob

回答

2
int check = 0; 
sqlite3_stmt *statement = nil; 
const char * sql; 

sql = "SELECT COUNT(*) FROM exhibitor WHERE exhibName = ?"; 

sqlite3_prepare_v2(db, sql, -1, &statement, NULL); 
sqlite3_bind_text(statement, 1, [[exhibitionArticle objectForKey:@"ex_name"] UTF8String], -1, SQLITE_TRANSIENT); 

while (sqlite3_step(statement) == SQLITE_ROW) { 

    check = sqlite3_column_int(statement, 0); 

} 

sqlite3_finalize(statement); 

if (check == 0) { 
    NSLog(@"Result of count is %d and so the data is not in the database", check); 
    //insert the data 

    NSDate *exhibAddDate = [NSDate date]; 
    NSString *exhibName = [exhibitionArticle objectForKey:@"ex_name"]; 
    NSString *exhibAbout = [exhibitionArticle objectForKey:@"ex_about"]; 
    NSString *exhibBooth = [exhibitionArticle objectForKey:@"ex_booth"]; 
    NSString *exhibAddress = [exhibitionArticle objectForKey:@"ex_address"]; 
    NSString *exhibTelephone = [exhibitionArticle objectForKey:@"ex_telephone"]; 
    NSString *exhibFax = [exhibitionArticle objectForKey:@"ex_fax"]; 
    NSString *exhibWebSite = [exhibitionArticle objectForKey:@"ex_website"]; 
    NSString *exhibEmail = [exhibitionArticle objectForKey:@"ex_email"]; 

    sql = "INSERT INTO exhibitor ('exhibAddDate', 'exhibName', 'exhibAbout', 'exhibBooth', 'exhibAddress', 'exhibTelephone', 'exhibFax', 'exhibWebSite', 'exhibEmail') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

    sqlite3_prepare_v2(db, sql, -1, &statement, NULL); 
    sqlite3_bind_text(statement, 1, [exhibAddDate UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(statement, 2, [exhibName UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(statement, 3, [exhibAbout UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(statement, 4, [exhibBooth UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(statement, 5, [exhibAddress UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(statement, 6, [exhibTelephone UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(statement, 7, [exhibFax UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(statement, 8, [exhibWebSite UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(statement, 9, [exhibEmail UTF8String], -1, SQLITE_TRANSIENT); 

    if (sqlite3_step(statement) == SQLITE_DONE){ 
     NSLog(@"Exhibit added"); 
    } else { 
     NSLog(@"Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(db)); 
    } 

} else { 
    NSLog(@"Result of count is %d and so the data is already in the database", check); 
} 

sqlite3_finalize(statement); 
sqlite3_close(db); 

嘗試是這樣的!

- > exhibName < - 在sql中必須是您想要搜索數據的列。

最好是把這一切放在你有的IBAction ..如果檢查== 0,執行插入。

請看看綁定變量,因爲你很容易受到sql注入的影響。這意味着如果我輸入類似於「blabla; DELETE * FROM exhibitor; blabla」作爲標題的例子,就會冒用戶刪除表中的所有數據的風險!綁定變量確保這不會發生。

不要XCODE,所以我不能檢查它是否全部正常工作。否則一直到你有它!

好運

+0

我真的很感激這段代碼,這段代碼對我來說工作得很好。非常感謝羅恩 –