首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >值在数据库中存储2次

值在数据库中存储2次
EN

Stack Overflow用户
提问于 2013-11-11 07:23:32
回答 1查看 38关注 0票数 0

我必须在数据库中插入x&y触摸坐标值。但它在数据库中存储了2次。当我长时间按下时,我会得到触摸坐标,并为button_tag插入一些标记。但是我只按了一次,为什么值要存储2次?

表:

代码语言:javascript
复制
button_tag   xcoor    ycoor
2             123.5    320.9
23            123.5    320.9


- (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;


    universal_button_tag = arc4random() % 99;

    NSLog(@"Universal Button tag: %d",universal_button_tag);

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


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


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



        const char *sql = [[NSString stringWithFormat:@"SELECT button_tag,xcoor,ycoor FROM touch where  button_tag='%d' AND xcoor = '%f' AND ycoor = '%f'",universal_button_tag,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){


            //Changes



            const char *sqlInsert = [[NSString stringWithFormat:@"insert into touch (button_tag,xcoor,ycoor) values ('%d','%f','%f')", universal_button_tag,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));


        }

    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-11 07:31:12

手势识别器的目标动作是针对手势识别器的不同状态调用的。您需要在您的方法中检查状态。

代码语言:javascript
复制
- (void)tapTest:(UILongPressGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        // handle completed gesture
    }
}

有关详细信息,请参阅UIGestureRecognizer的文档。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19900922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档