我在 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 模拟器上,这些查询中的每一个都返回与上面完全相同的数据:
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);
}