【问题标题】:sqlite3 - while loop is not executedsqlite3 - 不执行while循环
【发布时间】:2011-09-13 07:20:14
【问题描述】:

奇怪的问题请大家解决

当循环未执行时,在我读取的数据库代码中。

这是我的代码

NSString * query=@"select usernote from savenote where recipename like ? ";

            const char * sqlStatement=[query UTF8String];

            sqlite3_stmt *compiledStatement;

            sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);

            if (sqlite3_prepare_v2 (database, sqlStatement,-1,&compiledStatement,NULL)==SQLITE_OK)
            {
               sqlite3_bind_text(compiledStatement,1,[selString UTF8String],-1,SQLITE_STATIC);
                NSLog(@"selected:%@",selString);


                while (sqlite3_step(compiledStatement)== SQLITE_ROW)
                {


                    NSString * notz=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0)];
                    [Notes addObject:notz];

                    NSLog(@"n:%@",Notes);
                    txt.text=[Notes objectAtIndex:0];

                }

            }
        }

在上面的代码中,除了 while 循环永远不会被执行之外,一切正常。

感谢您阅读我的帖子。

【问题讨论】:

  • 您是否尝试过使用调试器来单步调试您的代码?可能包含 while 循环的 if 语句永远不会计算为真。如果没有,可能您对 sqlite3_prepare_v2() 的调用失败了...

标签: xcode4 sqlite


【解决方案1】:

我猜问题出在查询中。在like 子句中添加单引号(')。

"select usernote from savenote where recipename like '?'";

【讨论】:

    【解决方案2】:

    你可以关注this link

    在您的情况下,代码看起来像,

    const char * sqlStatement = "select usernote from savenote where recipename like ?001 ";
    
    sqlite3_bind_text(compiledStatement,1,[selString UTF8String],-1,SQLITE_TRANSIENT);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2021-01-16
      • 2014-09-10
      相关资源
      最近更新 更多