sqlite xcode 查询子句
Posted
技术标签:
【中文标题】sqlite xcode 查询子句【英文标题】:sqlite xcode query clause 【发布时间】:2013-02-24 07:56:16 【问题描述】:我对 sqlite 的查询遇到了一些奇怪的问题。 当我的 firstvar 值介于 30000 和 100000 之间时,我的查询不会返回任何内容。但是当 var 低于 30000 且大于 100000 时,它会。它在我的 xcode 应用程序中与在 sqlite 浏览器中相同。
SELECT * FROM OFFERS WHERE price > firstvar AND price < secondvar
这里是一些示例数据: http://www.purespain.com/example%20data.png
所以当我执行查询时
SELECT * FROM OFFERS WHERE price > 40000 AND price < 300000
我没有输出,但有很多价格 (prijs) 应该符合标准。
例如当我表演时
SELECT * FROM OFFERS WHERE price > 140000 AND price < 300000
或 SELECT * FROM OFFERS WHERE price > 0 AND price
那么我有正确的结果
执行查询的代码是:
-(NSMutableArray *)selectOffersWithOndergrens:(int)ondergrens andBovengrens:(int)bovengrens andSoort:(NSString *)soort
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES);
NSString *docpath = [path objectAtIndex:0];
dbPathString = [docpath stringByAppendingPathComponent:@"puurspanje.db"];
Offers = [[NSMutableArray alloc] init];
// Get all newsitems from the sqlite database
sqlite3_stmt *get_offers_stmt;
NSString *querysql = [[NSString alloc]init];
if (sqlite3_open([dbPathString UTF8String], &puurspanjeDB)==SQLITE_OK)
[Offers removeAllObjects];
if (![soort isEqual: @"alle"])
querysql = [NSString stringWithFormat:@"SELECT * FROM OFFERS WHERE prijs > %d AND prijs < %d AND soort = (SELECT TYPE_ID FROM TYPES WHERE NAME LIKE '%@' )", ondergrens, bovengrens, soort];
else
querysql = [NSString stringWithFormat:@"SELECT * FROM OFFERS WHERE prijs > %d AND prijs < %d", ondergrens, bovengrens];
const char* query_sql = [querysql UTF8String];
if (sqlite3_prepare(puurspanjeDB, query_sql, -1, &get_offers_stmt, NULL)==SQLITE_OK)
while (sqlite3_step(get_offers_stmt)==SQLITE_ROW)
NSString *listing_ID = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(get_offers_stmt, 1)];
NSString *titel = [[NSString alloc]initWithUTF8String:(const char * )sqlite3_column_text(get_offers_stmt, 2)];
NSString *omschrijving = [[NSString alloc]initWithUTF8String:(const char * )sqlite3_column_text(get_offers_stmt, 3)];
NSString *prijs = [[NSString alloc]initWithUTF8String:(const char * )sqlite3_column_text(get_offers_stmt, 4)];
NSString *referentie = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 5)];
NSString *slaapkamer = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 6)];
NSString *verdiep = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 7)];
NSString *badkamers = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 8)];
NSString *woonoppv = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 9)];
NSString *perceeloppv = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 10)];
NSString *kenmerken = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 11)];
NSString *buurtkenmerken = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 12)];
NSString *indeling = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 13)];
NSString *hypotheek = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 14)];
NSString *soort = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(get_offers_stmt, 15)];
Offer *offer = [[Offer alloc]initWithListing_ID:listing_ID andTitel:titel andOmschrijving:omschrijving andPrijs:prijs andReferentie:referentie andSlaapkamers:slaapkamer andVerdiep:verdiep andBadkamers:badkamers andWoonoppv:woonoppv andPerceeloppv:perceeloppv andKenmerken:kenmerken andBuurtKenmerken:buurtkenmerken andIndeling:indeling andHypotheek:hypotheek andSoort:soort];
[Offers addObject:offer];
return Offers;
我不知道可能是什么原因。任何帮助表示赞赏。
乔纳斯
【问题讨论】:
没有足够的信息来回答。显示示例数据以及预期和实际输出。 阅读FAQ. 我在我的主题中添加了一些信息。我希望问题现在已经清楚了 我没有看到price
列?
price = prijs 我的原始查询是 select* from offer where prijs > 40000
【参考方案1】:
原因是我选择文本作为数据类型而不是数字,并且因为文本中有空格,结果显示不正常。
【讨论】:
以上是关于sqlite xcode 查询子句的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Android SQLite 查询的动态 ArrayList 编写 WHERE IN 子句