gpt4 book ai didi

ios - 值在数据库中插入两次

转载 作者:行者123 更新时间:2023-11-29 03:32:03 25 4
gpt4 key购买 nike

我有 UIWebView 用于显示 HTML 文章页面。我使用 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

最佳答案

只需像这样更改您的选择查询

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

因此您需要在 WHERE 条件中使用 AND 操作来检查 x 和 y 坐标

关于ios - 值在数据库中插入两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19632146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com