难以从数据库中获取特定数据

时间:2012-03-13 10:39:01

标签: iphone select sqlite

我有一个表有两列作为组合(文本),标志(数字)。我正在尝试使用select query中的where子句根据组合值检索flag的值。

问题是当组合值从1开始时,例如其中combination ='1001'然后它从表中检索正确的flag值,但是当它从0开始时,例如其中combination ='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);

}

}

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:1)

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

更改代码

NSString * data = [self status:combination];

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