vendredi 4 mars 2016

How to fetch data from table in Sqlite in ios?

Hi I am working with the SQLite i am able to create the table and alter table but after adding columns to the table, data is not fetching.

upto now my implementation inserting table values like below

 NSString *sqlString=[NSString stringWithFormat:@"insert into %@(toid,fromid,msg,timedate,left,messageID,chatStatus) values('%@','%@','%@','%@',%d,'%@','%@')",tableName,userid,friendid,message,timedate,(int)leftright,messageID,chatStatus];

below i am fetching data from table

-(NSMutableArray *)AllRowFromTableName:(NSString *)tableName withUserID:(NSString *)userid withFriendID:(NSString *)friendid
{
NSMutableArray *array=[[NSMutableArray alloc] init];
NSString *sqlString=[NSString stringWithFormat:@"select *from chatTable where toid='%@' and fromid='%@'",userid,friendid];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &statement, nil)==SQLITE_OK) {
    while (sqlite3_step(statement)==SQLITE_ROW) {
        ChatHistorySkeleton *tempSkeleton=[[ChatHistorySkeleton alloc] init];

        tempSkeleton.u_id =[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 1)];//f_id
        // NSString *strn = [NSString stringWithFormat:@"%@",tempSkeleton.mToID];
        tempSkeleton.f_id=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
        tempSkeleton.msg=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
        //tempSkeleton.fromMe=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
        // tempSkeleton.fromMe=((int)sqlite3_column_int(statement, 4)) ?  YES : NO;
        tempSkeleton.lft_rght=(int)sqlite3_column_int(statement, 4);
        tempSkeleton.tim_dte=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 5)];
        //if messageID Null
        char *messageIDForEmptyCheck = (char *)sqlite3_column_text(statement, 6);
        NSString *messageID1 = messageIDForEmptyCheck?[[NSString alloc]initWithUTF8String:messageIDForEmptyCheck]:@"";
        tempSkeleton.messageID=messageID1;

        //if chat status Null
        char *chatStatus = (char *) sqlite3_column_text(statement,7);
        NSString *chatStatus1 = chatStatus?[[NSString alloc]initWithUTF8String:chatStatus]:@"";
        tempSkeleton.chatStatus=chatStatus1;
        [array addObject:tempSkeleton];

        //  NSLog(@"IN LOOP=>%@",array);

    }



}

return array;

}

i just alter table and added two columns messageID,chatstatus but these two values showing empty where i am missing can any body help me please

Aucun commentaire:

Enregistrer un commentaire