【问题标题】:Getting error while executing SQLite command from application从应用程序执行 SQLite 命令时出错
【发布时间】:2011-09-13 17:42:44
【问题描述】:

我的应用程序中有这个简单的功能:

-(NSMutableArray *)SelectProductID:(NSMutableArray *)arr
{
    NSLog(@"----------------");

    sqlite3_stmt *statement;

    NSMutableArray *arrPordID = [[NSMutableArray alloc]init];

    @try
    {

        //Get productID
        for(NSString *strSubProductID in arr)
        {


            NSString *s = [NSString stringWithFormat:@"SELECT ProductID FROM SubProducttable where SubProductID=%@",strSubProductID];

            const char *sql = [s cStringUsingEncoding:NSASCIIStringEncoding];

            if (sqlite3_prepare_v2(database, [s cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {

                while (sqlite3_step(statement) == SQLITE_ROW){

                    char *dbString;

                    dbString = (char *)sqlite3_column_text(statement, 0);
                    NSString *pID = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                    [arrPordID addObject:pID];

                }

            }             

        }
    }
    @catch (NSException *exception) {

        @throw exception;
    }
    @finally {

        sqlite3_finalize(statement);
    }
    return arrPordID;

}

我在这里遇到了一个奇怪的问题。当应用程序到达while (sqlite3_step(statement) == SQLITE_ROW){ 时,永远不会进入循环。我不知道为什么。我在 SQLite 管理器中执行了相同的查询(当应用程序未运行时)。我得到一个单一的结果。我得到的结果是2。但在这里我什么也得不到。

是的,每当我运行我的应用程序时,我总是在 SQLite 管理器中关闭数据库。我还清理了应用程序,重新启动了 XCode,并从模拟器中删除了应用程序。但没有成功。

在调试过程中我还看到了一件奇怪的事情。在调试时,sqlite3_stmt *statement 总是被跳过。这就是我没有得到任何结果的原因吗?

【问题讨论】:

  • 您在 SubProducttable 中的 SubProductID 是什么类型?
  • sqlite3_errcode()sqlite3_errmsg() 是你的朋友。向他们寻求帮助。

标签: iphone sqlite


【解决方案1】:

你试过单引号中的 subproductId 吗?

NSString *s = [NSString stringWithFormat:@"SELECT ProductID FROM SubProducttable where SubProductID='%@'",strSubProductID];

【讨论】:

  • 很奇怪。我从来没有这个问题。我从不使用单引号,但一切都很好。不知道为什么这次没有成功..!!
【解决方案2】:
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlQuery = @"SELECT ProductID FROM SubProducttable where SubProductID=%@",strSubProductID;
    sqlite3_stmt *statement;
    if(sqlite3_prepare_v2(database, statement, -1, &sqlQuery, NULL) == SQLITE_OK) {
        while(sqlite3_step(sqlQuery ) == SQLITE_ROW) {
            // Read the data and add to your array object
        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(statement);

}
sqlite3_close(database)

【讨论】:

  • 我在打开和关闭数据库的地方使用了单独的函数。这不是问题。因为在上述函数之前,我在一个函数中有一个类似的 SQLite 查询。它运行成功。
猜你喜欢
  • 2015-07-16
  • 2013-12-09
  • 1970-01-01
  • 2014-11-16
  • 2017-10-08
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多