Sqlite查询循环不工作
我想运行这个查询,但既不NSLog工作,也没有警报显示..查询不执行它不进入if循环
viewDidLoad{ pin_lat = pinAnnotation.coordinate.latitude; NSLog(@"pin latitude is %f",pin_lat); //This one is only working sqlite3_stmt *statement; NSString *qsql = [NSString stringWithFormat:@"SELECT name FROM site WHERE latitude LIKE %f AND longitude LIKE %f",pin_lat,pin_long]; if (sqlite3_open([qsql UTF8String], &db) == SQLITE_OK){ // sqlite3_exec(db, [statement UTF8String],NULL, NULL, &err == SQLITE_OK); NSLog(@"QUERY IS WORKING IN OPEN DB"); if (sqlite3_prepare_v2(db, [qsql UTF8String], -1, &statement, nil)== SQLITE_OK) { NSLog(@"QUERY IS WORKING IN DB PREPARE"); while(sqlite3_step(statement)==SQLITE_ROW) { /* char *field1 = (char *) sqlite3_column_text(statement, 3); getting name value in field1 NSString *field1Str = [[NSString alloc] initWithUTF8String: field1]; [field1Str release];*/ NSLog(@"QUERY IS WORKING"); UIAlertView *alert_pin = [[UIAlertView alloc] initWithTitle:@"" message:@"Cannot create a duplicate location" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Done", nil]; [alert_pin show]; [alert_pin release]; } //—-deletes the compiled statement from memory—- sqlite3_finalize(statement); } else{ [mapView addAnnotation:pinAnnotation]; // this is working ... but i want the if loop to also work } } sqlite3_close(db); // not exist to show alert }
我的查询有什么问题为什么它不执行查询和打开数据库或打印日志里面的if循环…任何一个可以纠正我的代码..任何帮助?
Midhun是正确的。 我进一步build议,这些if
语句中的每一个else
应该有一个相应的else
子句,显示返回码以及可能的情况下的错误信息。 或者等同地检查每个步骤的故障,如果是,则报告诊断信息并返回。 因此,它可能看起来像:
int rc; sqlite3_stmt *statement; NSString *qsql = [NSString stringWithFormat:@"SELECT name FROM site WHERE latitude LIKE %f AND longitude LIKE %f",pin_lat,pin_long]; if ((rc = sqlite3_open([path UTF8String], &db)) != SQLITE_OK) { NSLog(@"%s sqlite3_open failed: (%d)", __FUNCTION__, rc); return; } NSLog(@"QUERY IS WORKING IN OPEN DB"); if ((rc = sqlite3_prepare_v2(db, [qsql UTF8String], -1, &statement, nil)) != SQLITE_OK) { NSLog(@"%s sqlite3_prepare_v2 failed: \"%s\" (%d)", __FUNCTION__, sqlite3_errmsg(db), rc); sqlite3_close(db); return; } NSLog(@"QUERY IS WORKING IN DB PREPARE"); while ((rc = sqlite3_step(statement)) == SQLITE_ROW) { const char *field0 = (const char *)sqlite3_column_text(statement, 0); // getting name value in field1 NSString *field0Str = [NSString stringWithUTF8String:field0]; NSLog(@"QUERY IS WORKING: %@", field0Str); UIAlertView *alert_pin = [[UIAlertView alloc] initWithTitle:@"" message:@"Cannot create a duplicate location" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Done", nil]; [alert_pin show]; [alert_pin release]; } if (rc != SQLITE_DONE) { NSLog(@"%s sqlite3_step failed: \"%s\" (%d)", __FUNCTION__, sqlite3_errmsg(db), rc); sqlite3_finalize(statement); sqlite3_close(db); } if ((rc = sqlite3_finalize(statement)) != SQLITE_OK) { NSLog(@"%s sqlite3_finalize failed: \"%s\" (%d)", __FUNCTION__, sqlite3_errmsg(db), rc); sqlite3_finalize(statement); sqlite3_close(db); } sqlite3_close(db);
这一行导致这个问题:
if (sqlite3_open([qsql UTF8String], &db) == SQLITE_OK)
上述代码中的参数不正确。
您正在传递SQL查询而不是数据库path。
因为这个数据库没有打开,并且if条件失败。 这就是为什么你没有得到任何输出。
将数据库path作为sqlite3_open
第一个parameter passing。 它会工作。