Sqlite查询循环不工作 [英] Sqlite Query Loop not working

查看:180
本文介绍了Sqlite查询循环不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行这个查询,但是没有NSLog正在工作,也没有警告正在显示..查询不执行它不进入if循环

i am trying to run this query but neither NSLog is working nor the alert is showing .. query does not executes it does not gets into the if loop

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
}

查询为什么它不执行查询和打开DB或打印日志在if循环...可以任何一个更正我的代码..任何帮助?

what is wrong with my query why is it not executing the query and opening the DB or printing the Log's inside the if loop ... can any one correct my code .. any help?

推荐答案

Midhun是正确的。我进一步建议,这些 if 语句应该有一个相应的 else 子句,显示返回码如果可能,错误消息。或者,等同地,在每个步骤检查失败,并且如果是,报告诊断信息并返回。因此,它可能如下所示:

Midhun is correct. I'd further advise that each of these if statements should have a corresponding else clause that displays the return code as well as, if possible, the error message. Or, equivalently, check for failure at each step and, if so, report diagnostic information and return. Thus, it might look like:

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

这篇关于Sqlite查询循环不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆