开发者

Query is not retrieving the data from table

开发者 https://www.devze.com 2023-04-10 10:47 出处:网络
im trying to integrate a database into my project.This is for first time im implementing a database project.I integrated the database into my project.Database has 3 columns named rowid,col1,col2.Below

im trying to integrate a database into my project.This is for first time im implementing a database project.I integrated the database into my project.Database has 3 columns named rowid,col1,col2.Below code i used for acessing the datafrom database.But it is not enetering in "if loop"Can anyone help me where im doing wrong in this case.Thanks in advance.![enter image description here][1]

 /*   -(void)print
    {
        sqlite3_stmt *statement;
        qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = '%d'",1];

        if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {


             NSLog(@"hhhhhhhhh");
            while (sqlite3_step(statement) == (SQLITE_ROW)){

                   NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];


               // tv.text=qsql;



            }

        }}*/
    -(void)print
    {
        sqlite3_stmt *statement;
        //qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %d",1];
        qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %i",1];

        //const char *sqlStatement = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
       const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
        if(sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL) == SQLITE_OK) {


            NSLog(@"hhhhhhhhh");
            while (sqlite3_step(statement) == (SQLITE_ROW)){
              //  NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
                NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%i",sqlite3_column_int(statement, 0)]];

                // tv.text=qsql;
            }

        }            

            }

    -(void) checkAndCreateDatabase{
        BOOL succe开发者_如何学Css;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        success = [fileManager fileExistsAtPath:databasePath];
        if(success) return;
        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
        [fileManager release];
    }

    -(void) openDB {

        databaseName = @"Lighthr.sqlite";
        NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDir = [documentsPaths objectAtIndex:0];   
        databasePath = [documentDir stringByAppendingPathComponent:databaseName];
        [self checkAndCreateDatabase];  


        if(sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK){
            NSLog(@"coming here???");
        }
    }


  [1]: http://i.stack.imgur.com/lm1ZV.png


because rowid is always integer and you are trying to access using string.
I thing you have to try this.

Edit

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %i",1];
    const char *sqlStatement = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK) {


        NSLog(@"hhhhhhhhh");
        while (sqlite3_step(statement) == (SQLITE_ROW)){
            NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
           // tv.text=qsql;
        }

    }
}


Are you trying to test without your "where" clause ?

And what is the error code return by sqlite3_prepare_v2 ?

int sql_answer = sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL);
NSLog(@" SQL answer : %d",sql_answer );
if(sql_answer == SQLITE_OK) {
  ...


i think you should do in this way .

-(void)print {

sqlite3_stmt *statement; qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %d",1];

const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL) == SQLITE_OK) {


    NSLog(@"hhhhhhhhh");
    while (sqlite3_step(statement) == (SQLITE_ROW)){            
        NSString *Q_NO =[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]



    }


NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];

above stameny replace with below statement

NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%i",sqlite3_column_int(statement, 0)]];

and

const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];

now replace [qsql UTF8String] with qsql_stament. It will work fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消