2013-10-28 120 views
0

我有用於顯示HTML文章頁面的UIWebView。我使用UILongPressGesture獲取觸摸位置座標。我將這些座標保存到數據庫。但是在數據庫中插入兩次保存值。爲什麼?值在數據庫中插入兩次

- (void)tapTest:(UILongPressGestureRecognizer *)sender { 
    NSLog(@"coordinate is %f %f", [sender locationInView:wbCont.scrollView].x, [sender locationInView:wbCont.scrollView].y); 


    xcor = [sender locationInView:wbCont.scrollView].x; 
    ycor = [sender locationInView:wbCont.scrollView].y; 


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    // NSString *documentsDirectory = [paths objectAtIndex:1]; 

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"os.sqlite"]; 
    NSLog(@"filepath %@",path); 


    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) { 

     const char *sql = [[NSString stringWithFormat:@"SELECT xcoor FROM touch where xcoor = '%f','%f'",xcor,ycor] cStringUsingEncoding:NSUTF8StringEncoding]; 

     NSLog(@"sql is %s",sql); 

     BOOL favExist = false; 

     sqlite3_stmt *statement, *addStmt; 

     if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) { 
      // We "step" through the results - once for each row. 
      while (sqlite3_step(statement) == SQLITE_ROW) { 



       favExist = true; 
      } 
     } 


     if(!favExist){ 



      const char *sqlInsert = [[NSString stringWithFormat:@"insert into touch (xcoor,ycoor) values ('%f','%f')", xcor,ycor] cStringUsingEncoding:NSUTF8StringEncoding]; 

      NSLog(@"sql insert is %s",sqlInsert); 


      // [catID release]; 

      if(sqlite3_prepare_v2(database, sqlInsert, -1, &addStmt, NULL) != SQLITE_OK) 
       NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); 

      NSLog(@"error is %s",sqlite3_errmsg(database)); 

      if(SQLITE_DONE != sqlite3_step(addStmt)) 
       NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database)); 


     }else { 


     } 

    } 

} 

enter image description here

回答

1

只要改變你的選擇這樣的查詢

const char *sql = [[NSString stringWithFormat:@"SELECT xcoor FROM touch where xcoor = '%f' AND ycoor = '%f'",xcor,ycor] cStringUsingEncoding:NSUTF8StringEncoding]; 

因此,你需要和opertion在WHERE條件檢查x和y座標

+0

yes..thanks。 ... – user2807197