我有一个表格,它有两列作为组合(文本),标志(数字)。我试图检索标志的值取决于组合值使用选择查询中的where子句。从数据库中提取特定数据时遇到困难
问题是,当组合值从1开始时,例如,在组合='1001'的情况下,它从表中检索标志的正确值,但是当它从0开始时例如如果组合='0010',则不会从数据库中检索到任何内容。我调试了代码,但没有得到原因。以下是数据库映像。
-(void)fetchfromDatabase
{
sqlite3 *database;
NSString *strPath = [[NSBundle mainBundle]pathForResource:@"ken" ofType:@"db"];
NSLog(@"DBPath : %@",strPath);
const char *dbPath = [strPath UTF8String];
if(sqlite3_open(dbPath, &database)==SQLITE_OK)
{
NSLog(@"Database is Ok");
// const char *cConcate = [concate UTF8String];
NSString *query = [NSString stringWithFormat:@"select * from kentable where combination =%@",concate];
NSLog(@"final concate in database:%@",concate);
NSLog(@"Query:%@",query);
const char *sqlQuery =[query cStringUsingEncoding:NSASCIIStringEncoding];
NSLog(@"char query %s", sqlQuery);
sqlite3_stmt *queryStatement;
if(sqlite3_prepare_v2(database, sqlQuery,-1,&queryStatement, NULL)==SQLITE_OK)
{
NSLog(@"Conversion is Ok");
while (sqlite3_step(queryStatement)==SQLITE_ROW)
{
NSLog(@"in While Statement of row");
int flag=sqlite3_column_int(queryStatement, 1);
NSLog(@"question 5:%d",flag);
}
sqlite3_reset(queryStatement);
}
else
{
NSLog(@"ERROR");
}
sqlite3_close(database);
}
}
我想你可能想在你的查询中加引号。尽管我更喜欢使用CoreData的sqlite。 – govi 2012-03-13 11:07:12
使用核心数据!速度更快,您将拥有更少的代码进行管理。你不会写选择语句 – 2012-03-13 11:08:26
@govi你是对的。非常感谢。它运行良好。 – 2012-03-14 10:12:10