-1

我正在尝试运行此查询,但 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 循环中打印日志……任何人都可以更正我的代码……有什么帮助吗?

4

2 回答 2

1

弥敦是对的。我进一步建议这些if语句中的每一个都应该有一个相应的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);
于 2012-12-22T14:35:24.140 回答
0

此行导致问题:

 if (sqlite3_open([qsql UTF8String], &db) == SQLITE_OK)

上面代码中的参数不正确。

您正在传递 sql 查询而不是数据库路径。

因为这个数据库没有打开,if 条件失败。这就是为什么你没有得到任何输出。

将数据库路径作为sqlite3_open. 它会工作的。

于 2012-12-22T13:36:21.503 回答