2012-03-13 146 views
0

我有一个表格,它有两列作为组合(文本),标志(数字)。我试图检索标志的值取决于组合值使用选择查询中的where子句。从数据库中提取特定数据时遇到困难

问题是,当组合值从1开始时,例如,在组合='1001'的情况下,它从表中检索标志的正确值,但是当它从0开始时例如如果组合='0010',则不会从数据库中检索到任何内容。我调试了代码,但没有得到原因。以下是数据库映像。

This is table having two columns.

-(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); 

} 

}

+1

我想你可能想在你的查询中加引号。尽管我更喜欢使用CoreData的sqlite。 – govi 2012-03-13 11:07:12

+0

使用核心数据!速度更快,您将拥有更少的代码进行管理。你不会写选择语句 – 2012-03-13 11:08:26

+0

@govi你是对的。非常感谢。它运行良好。 – 2012-03-14 10:12:10

回答

1

我也有这样的任务..它运作良好...只是通过

的NSString更改代码*数据= [自我状态:组合];

-(NSString *) status:(NSString *)concate 
    { 
    databasePath = [[NSString alloc] initWithString: [[NSBundle mainBundle] pathForResource:@"ken" ofType:@"db"]]; 
     sqlite3_stmt *statement; 
     NSString *aName; 
     const char *dbpath = [databasePath UTF8String]; 

     if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
     { 
      NSString *querySQL = [NSString stringWithFormat: 
            @"SELECT combination, flag FROM details WHERE combination =\"%@\"", 
            concate]; 

      const char *query_stmt = [querySQL UTF8String]; 
      sqlite3_prepare_v2(database, 
           query_stmt, -1, &statement, NULL); 
      if (sqlite3_step(statement) == SQLITE_ROW) 
      { 
       // Read the data from the result row 
       aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]; 

       NSLog(@"same %@" ,aName) ; 

      } 
      else 
      { 
       aName=nil; 
      } 

    sqlite3_finalize(statement); 
      sqlite3_close(database); 

     } 


[databasePath release]; 
    return aName; 
} 
3

尝试更改代码如下。不要介意,但是如你所做的那样准备sql查询更好地遵循标准惯例。你可以使用sqlite3_bind_text函数来绑定字符串。

if(sqlite3_open(dbPath, &database)==SQLITE_OK) 
{ 
    const char *sqlQuery = "select * from kentable where combination = ?"; 
    sqlite3_stmt *queryStatement; 

    if(sqlite3_prepare_v2(database, sqlQuery,-1,&queryStatement, NULL)==SQLITE_OK) 
    { 
     NSLog(@"Conversion is Ok"); 
     sqlite3_bind_text(insertStmt,1, [concate UTF8String], -1, SQLITE_TRANSIENT);  

     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); 
} 
+0

这是否帮助你? – 2012-03-15 12:52:01

相关问题