Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone sqlite3查询在cmd行上工作,但在iOS 6模拟器中不返回任何数据_Iphone_Ios_Xcode_Sqlite - Fatal编程技术网

Iphone sqlite3查询在cmd行上工作,但在iOS 6模拟器中不返回任何数据

Iphone sqlite3查询在cmd行上工作,但在iOS 6模拟器中不返回任何数据,iphone,ios,xcode,sqlite,Iphone,Ios,Xcode,Sqlite,我在iOS 6 iPad应用程序中使用了这个sqlite3表: CREATE TABLE notes(id INTEGER PRIMARY KEY, note TEXT, noteDate TEXT, wasUploaded INTEGER); 在sqlite3命令行中,此查询工作: sqlite> Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `wasUploaded`=0; 1|Well|2012-10

我在iOS 6 iPad应用程序中使用了这个sqlite3表:

CREATE TABLE notes(id INTEGER PRIMARY KEY, note TEXT, noteDate TEXT, wasUploaded INTEGER);
在sqlite3命令行中,此查询工作:

sqlite> Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `wasUploaded`=0;

1|Well|2012-10-04 22:46:23|0
在iOS iPad 6.0模拟器上,每个查询返回的数据与上述数据完全相同:

const char *sqlStatement = "Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `id`=1";

const char *sqlStatement = "Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `note`='Well'";

const char *sqlStatement = "Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `noteDate`='2012-10-04 22:46:23'";
但是这个在命令行上运行良好的查询现在不会返回任何数据:

const char *sqlStatement = "Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `wasUploaded`=0";
我感到困惑。为什么最后一个查询不起作用?我需要把那个列作为索引还是什么?其他两个非索引列可以工作,但这两个列不能工作

没有错误。最后一个不返回数据的查询给出了正常的返回代码101(sqlite3_step()已完成执行),而没有where子句的查询返回的数据与其他三个查询相同

编辑:这是完整的代码

- (NSString *)getNotesToBeUploaded {
sqlite3 *stuDb;
NSString *thisNote;
NSMutableString *notes = [[NSMutableString alloc]init];

if (self.filePath == @"empty") {
    [self setDatabaseFilePath];
}

if (sqlite3_open([self.filePath UTF8String], &stuDb) == SQLITE_OK)
{
    // this is the query line that get changed to show stackoverflow the different results:
    const char *sqlStatement = "Select `id`,`note`,`noteDate` FROM notes WHERE `wasUploaded`=0";
    sqlite3_stmt *compiledStatement;
    int nResult = sqlite3_prepare_v2(stuDb, sqlStatement, -1, &compiledStatement, NULL);
    if ( nResult == SQLITE_OK)
    {
        int nret; // diagnostic used to watch return vaues when single stepping
        while ((nret = sqlite3_step(compiledStatement)) == SQLITE_ROW)
        {
            int id =  sqlite3_column_int(compiledStatement, 0);
            const unsigned char *note =  sqlite3_column_text(compiledStatement, 1);
            const unsigned char *noteDate =  sqlite3_column_text(compiledStatement, 2);
            int wu =  sqlite3_column_int(compiledStatement, 4);
            if (strlen((const char *)note) > 0 && strlen((const char *)noteDate) > 0)
            {
                thisNote = [NSString stringWithFormat:@"%d,%s,%s,%d\n",id, noteDate, note, wu];
                [notes appendString:thisNote];
            }
        }
    } else {
        sqlite3_finalize(compiledStatement);// prevent small memory leaks
        sqlite3_close(stuDb);
        thisNote =
        [NSString stringWithFormat:@"prepare failed with status:%d in %s at line %d path was %@,0,0\n",nResult,__FILE__,__LINE__,self.filePath];
        [notes appendString:thisNote];
        [notes appendString:@"\n"];
        return (NSString *)notes;
    }
    sqlite3_finalize(compiledStatement);
    sqlite3_close(stuDb);
}

您是否正在检查
sqlite3
调用的返回代码?而且,如果您没有得到
SQLITE\u OK
SQLITE\u ROW
,您应该检查
sqlite3\u errmsg
结果以诊断发生了什么。如果你想让我们帮助你,你真的应该分享你的代码。 但首次使用iOS SQLite应用程序时最常见的问题是

  • 未能将数据库包括在应用程序包中。检查目标设置,确保已将数据库包括在
    构建阶段中。您还可以通过查看模拟器中
    ~/Library/Application Support/iPhone simulator
    文件夹中的应用程序包来确认这一点。如果您想这样做,您可以通过在终端命令行界面中键入
    chflags no hidden~/Library
    来取消隐藏您的
    ~/Library
    文件夹(如果您没有)

  • 如果您计划从应用程序更新数据库,在尝试开始使用之前,未能首先将数据库从捆绑包复制到
    文档
    文件夹

  • 使用
    sqlite3\u open
    并将成功的返回代码解释为数据库已成功打开的证据。。。但是如果它没有找到数据库,
    sqlite3\u open
    函数将创建一个新的空白数据库。。。我总是建议人们改用
    sqlite3\u open\u v2
    ,在这种情况下,如果找不到该参数,您可以省略该参数来创建一个空白数据库,如果这不是您想要的

当然,也可能存在大量与代码相关的问题(调用函数的顺序、无法检查返回代码等)。在没有看到代码的情况下,不可能进一步评论

我觉得有义务分享我最后的SQLite编程建议,值得一看,这大大简化了iOS中的SQLite编程


更新:

看过你的代码后,它看起来不错。我刚刚运行了它(只是调整为只
NSLog
,而不是添加注释):

我得到的结果是:

2012-10-05 15:44:06.075 test8[1574:c07] 1,2012-10-05 19:43:37,ABC,0
2012-10-05 15:44:06.076 test8[1574:c07] 2,2012-10-05 19:43:46,XYZ,0

所以问题一定出在数据库本身。从你上次的评论来看,这听起来像是重建数据库为你做的。太好了。

执行查询时,您是否有任何错误?如果您省略WHERE子句,会发生什么情况?感谢我编辑了问题以回答您的问题此记录的
typeof(wassuplated)
有什么不同吗?很高兴知道这很简单。我不是故意把你从碎玻璃里拖出来的…:)谢谢我很犹豫是否要指出这一点,但您的回答似乎是一个剪贴式的“固定回答”,除了寻求更多的代码之外,没有任何部分与这个问题相关。事实上,这个问题已经回答了您提到的所有其他问题,它显示了返回的代码,还显示了一些查询已经工作。在我发布了一个扩展版本后,我希望您能够提供帮助。很抱歉,您觉得它没有帮助。如果它感觉像罐头(即使我只是把它打出来),那是因为在没有代码的情况下,我们仅限于广泛的泛化,而这些正是新sqlite程序员遇到的问题。我很高兴听到您确认您的
sqlite3\u步骤
正在返回
SQLITE\u行
,并且没有
sqlite3\u errmsg
可供检查。祝你好运。你愿意把数据库上传到某个地方吗?我们可以检查一下。整个数据库在问题的前五行中有完整的描述,因此你可以比我上传到某个地方更快地重新创建它。是的。这就是为什么我感到困惑。这太简单了,本该奏效的。谢谢
2012-10-05 15:44:06.075 test8[1574:c07] 1,2012-10-05 19:43:37,ABC,0
2012-10-05 15:44:06.076 test8[1574:c07] 2,2012-10-05 19:43:46,XYZ,0