3

I need to convert const char type data to NSString object type because sqlite database return char * type data. For that I have used NSString Class method stringWithUTF8strin but it is returning NULL.It fetches sqlite text type data.

Here is my code:

-(void)getAllData:(const char *)sqlQuery
{
name=[[NSMutableArray alloc]init];

department=[[NSMutableArray alloc]init];
image=[[NSMutableArray alloc]init];
testimonial=[[NSMutableArray alloc]init];
@try
{

NSArray *pathArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *final_phone_path=[[pathArray objectAtIndex:0]stringByAppendingPathComponent:@"amarinf5_excel.sqlite"];
NSLog(@"%@",final_phone_path);

NSFileManager *myFileManager=[NSFileManager defaultManager];

if(![myFileManager fileExistsAtPath:final_phone_path])
{
    NSLog(@"file doesnot exist");
}
if( sqlite3_open([final_phone_path UTF8String], &dataBAse)!=SQLITE_OK)
{
   NSLog(@"could not be able to open the file");
}
const char *sql=sqlQuery;
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(dataBAse, sql, -1, &statement, nil)!=SQLITE_OK)
{
    NSLog(@"Error message fail to prepare statement %s",sqlite3_errmsg(dataBAse));
}


while(sqlite3_step(statement)==SQLITE_ROW)
{
    const char *name1=(char *)sqlite3_column_text(statement, 0);
    const char *deprtment1=(char *)sqlite3_column_text(statement, 1);
    const char *image1= (char *)sqlite3_column_text(statement, 2);
    **const char *testimonial1=(char *)sqlite3_column_text(statement, 3);
    NSLog(@"%@",[NSString stringWithUTF8String:testimonial1]);**
    NSLog(@"%s",testimonial1);
    NSLog(@"%@",[NSString stringWithUTF8String:image1]);
    [name addObject:[NSString stringWithUTF8String:name1]];
    [department addObject:[NSString stringWithUTF8String:deprtment1]];
    [image addObject:[NSString stringWithUTF8String:image1]];

    //[testimonial addObject:[NSString stringWithUTF8String:testimonial1]];
    NSLog(@"%@",[NSString stringWithUTF8String:testimonial1]);
}
if (sqlite3_finalize(statement) != SQLITE_OK)
{
    NSLog(@"Failed to finalize data statement, normally error handling here.");
}
if (sqlite3_close(dataBAse) != SQLITE_OK)
{
    NSLog(@"Failed to close database, normally error handling here.");
}
}
@catch (NSException *exception) {
NSLog(@"An exception occurred: %@", [exception reason]);

}
@finally {

}

}
Bikram Thapa
  • 1,329
  • 1
  • 16
  • 29
  • http://stackoverflow.com/questions/10284011/convert-const-char-to-nsstring-and-convert-back-nsautoreleasenopool And http://stackoverflow.com/questions/4366799/nsstring-and-const-char-conversion – iPatel Jun 06 '14 at 08:50
  • this happen only for testimonial1 – pratik371987 Jun 06 '14 at 08:51
  • Thank you.
    Maulik M. Kamani
    ASP.net Developer
    Tech Sture Technology Pvt. Ltd the data i get in testimonial1 but could not be able to covert is there any possibility that it contains html tags
    – pratik371987 Jun 06 '14 at 09:07

3 Answers3

2

when i was tying different methods of NSString solution found using stringWithFormat method here is my code below

 while(sqlite3_step(statement1)==SQLITE_ROW)
{
    const char *name1=(char *)sqlite3_column_text(statement1, 0);
    const char *deprtment1=(char *)sqlite3_column_text(statement1, 1);
    const char *image1= (char *)sqlite3_column_text(statement1, 2);
    const char *testimonial1=(char *)sqlite3_column_text(statement1, 3);


    [name addObject:[NSString stringWithUTF8String:name1]];
    [department addObject:[NSString stringWithUTF8String:deprtment1]];
    [image addObject:[NSString stringWithUTF8String:image1]];
    [testimonial addObject:[NSString stringWithFormat:@"%s",testimonial1]];

}
0

I do the following and all goes right:

if (sqlite3_prepare_v2(database, [querySQL UTF8String], -1, &statement, nil) == SQLITE_OK) {
                    while (sqlite3_step(statement) == SQLITE_ROW) {
                        int idMessage = sqlite3_column_int(statement, 0);
                        char *nameChars = (char *) sqlite3_column_text(statement, 1);
                        char *methodChars = (char *) sqlite3_column_text(statement, 2);
                        char *bodyChars = (char *) sqlite3_column_text(statement, 3);
                        char *pathChars = (char *) sqlite3_column_text(statement, 4);
                        char *headerChars = (char *) sqlite3_column_text(statement, 5);
                        char *typebodyChars = (char *) sqlite3_column_text(statement, 6);
                        char *typebodyoutChars = (char *) sqlite3_column_text(statement, 7);
                        NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
                        NSString *method = [[NSString alloc] initWithUTF8String:methodChars];
                        NSString *body = [[NSString alloc] initWithUTF8String:bodyChars];
                        NSString *path = [[NSString alloc] initWithUTF8String:pathChars];
                        NSString *header = [[NSString alloc] initWithUTF8String:headerChars];
                        NSString *typebody = [[NSString alloc] initWithUTF8String:typebodyChars];
                        NSString *typebodyout = [[NSString alloc] initWithUTF8String:typebodyoutChars];
                    }
                    sqlite3_finalize(statement);
                }
Mikel Sanchez
  • 2,870
  • 1
  • 20
  • 28
0

There a 3 ways to convert a const char* to NSString in objective-c

+ (instancetype)stringWithUTF8String:(const char *)nullTerminatedCString; //for UTF8 string only
- (instancetype)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
+ (instancetype)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;

Verify your const char*, if it does not contains null char ('\0') at the end, add this char into the string cause these functions require nullterminated cs string. When your define a cs, the null char is automatically inserted at the and of string

const char *str = "some string";

but if you manipulate with char array, you have to do your self

Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44