【问题标题】:sqlite3 is not working in my iphone applicationsqlite3 在我的 iphone 应用程序中不起作用
【发布时间】:2010-08-18 22:00:40
【问题描述】:

我想在我的 iphone 应用程序中使用 sqlite3。但它不起作用。场景是:- 在按钮的单击事件上,一个方法应该运行并从数据库中检索数据。但是我的程序序列没有进入

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    const char *sqlStatement = "select * from records";
    sqlite3_stmt *compiledStatement;
    0NSLog(@"%s", sqlite3_errmsg(database)); // Results - no error
-
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)    {
//here NSLog statement does not work.

打开数据库没有问题。应该是什么原因?任何可行的解决方案。 谢谢

【问题讨论】:

    标签: iphone objective-c sqlite


    【解决方案1】:

    试试这个:NSLog(@"%s", sqlite3_errmsg(database));,看看你的查询是否有错误。

    【讨论】:

    • 没有错误...我编辑了我的问题,您可以有更好的感知。
    • 从您的代码中可以看出,您的 if 语句没有返回 SQLITE_OK。我使用 sqlite3 并将其分成两个块,首先我进行准备,没有 if,然后我使用 while(sqlite3_step(compiledStatement) == SQLITE_ROW) 循环遍历结果。试试看它是否有任何改变。
    • @alis:你的 NSLog 放错了地方。你想看看prepare的错误信息是什么。把它放在 if 的 else 部分。
    • 嗯,是的,你需要在准备之后有 NSLog,你不需要在 if 中有 NSLog
    • @Simon, @JeremyP:我按照你的指示,我得到了错误没有这样的表:记录(我的表名)。但是我的数据库有一个名为 records 的表,它也不是空的。那么,这背后的原因是什么?
    【解决方案2】:

    这是我自己的代码的修改示例:

      NSString *file = @"youdatabase.db"
      sqlite3 *db;    
      if(sqlite3_open([file UTF8String], &db) == SQLITE_OK)
      {
        NSString *req = @"SELECT * FROM totoTable";
        sqlite3_stmt *returnReq;
    
        if(sqlite3_prepare(db, [req cStringUsingEncoding:NSUTF8StringEncoding], -1, &returnReq, NULL) == SQLITE_OK)
        {
          while (sqlite3_step(returnReq) == SQLITE_ROW)
          {
             //Printf first returned value
            NSLog(@"%s", (char*)sqlite3_column_text(returnReq, 0));
          }     
        }
        sqlite3_close(db);
      }
    

    希望对你有帮助;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-23
      • 2019-01-25
      • 1970-01-01
      • 2012-09-25
      • 2017-11-22
      • 2012-06-05
      • 2011-07-02
      • 1970-01-01
      相关资源
      最近更新 更多