gpt4 book ai didi

iphone - sqlite3查询可在命令行上运行,但在iOS 6 SImulator中不返回任何数据

转载 作者:行者123 更新时间:2023-12-03 18:38:38 24 4
gpt4 key购买 nike

我在iPad的iOS 6应用中使用了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 Simulator上,这些查询中的每一个都返回与上面完全相同的数据:
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_OKSQLITE_ROW,则应检查sqlite3_errmsg结果以诊断正在发生的情况。如果您希望我们为您提供帮助,那么您真的应该共享您的代码。
但是,首次iOS SQLite应用程序中最常见的问题是

  • 无法将数据库包含在应用程序的捆绑软件中。检查您的目标设置,并确保已将数据库包含在Build Phases中。您也可以通过在~/Library/Application Support/iPhone Simulator文件夹中的模拟器中查看应用程序的捆绑包来确认这一点。如果要这样做,则可以通过在终端命令行界面中键入~/Library来取消隐藏chflags no hidden ~/Library文件夹。
  • 如果您打算从应用程序更新数据库,则在尝试开始使用数据库之前,无法先将数据库从捆绑软件复制到Documents文件夹。
  • 使用sqlite3_open并将成功的返回代码解释为成功打开数据库的证据...但是,如果找不到数据库,则sqlite3_open函数会烦人地创建一个新的空白数据库...我总是建议人们使用sqlite3_open_v2相反,如果找不到该数据库,则可以省略该参数以创建一个空白数据库。

  • 当然,也可能存在大量与代码相关的问题(调用函数的顺序,无法检查返回代码等)。不看代码就不可能进一步注释。

    我有义务分享我的最终SQLite编程建议,那就是值得一试 FMDB Objective-C SQLite wrapper library,它大大简化了iOS中的SQLite编程。

    更新:

    看完您的代码,看起来不错。我只是运行它(仅调整为 NSLog而不是附加注释):
    - (void)test2
    {
    sqlite3 *stuDb;
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *databaseName = [documentsDirectory stringByAppendingPathComponent:@"test.db"];

    if (sqlite3_open([databaseName 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];
    NSLog(@"%d,%s,%s,%d\n",id, noteDate, note, wu);
    }
    }
    } else {
    //sqlite3_finalize(compiledStatement);// prevent small memory leaks, not needed if the prepare failed
    sqlite3_close(stuDb);
    NSLog(@"prepare failed with error %s", sqlite3_errmsg(stuDb));
    return;
    }
    sqlite3_finalize(compiledStatement);
    sqlite3_close(stuDb);
    }
    }

    我得到了结果:
    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

    因此问题必须出在数据库本身上。从您的最后一条评论来看,听起来好像重建数据库为您完成了任务。那很棒。

    关于iphone - sqlite3查询可在命令行上运行,但在iOS 6 SImulator中不返回任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12751629/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com