1

I have sqlite db and export some result with query

sqlite3 *MyDB;
const char *dbpath = [StringPathDB UTF8String]; // Convert NSString to UTF-8
NSString *querySQL = @"select * from chat order by id desc limit 20";



if (sqlite3_open(dbpath, &MyDB) == SQLITE_OK)
{

    const char *sql = [querySQL UTF8String];

    sqlite3_stmt *searchStatement;

    if (sqlite3_prepare_v2(MyDB, sql, -1, &searchStatement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(searchStatement) == SQLITE_ROW)
        {
            NSString *nickname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 0)];
            NSString *time = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 1)];
            NSString *text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 2)];

Then I put all in NSMutableArray

NSMutableArray *list = [[NSMutableArray alloc] init];

[list addObject:nickname];
[list addObject:time];
[list addObject:text];

When I try to save al in file with this code

for(int i = 0; i < [list count]; i++) {

                    NSLog(@"%@",list);
                   [list writeToFile:filePATH atomically:YES];
                   break;
                }

It stamps correctly in NSLog but in file saves only the last record not all array. Please help me!

3
  • 1
    What is filePATH? How are you creating it?
    – Undo
    Nov 23, 2013 at 16:31
  • Is everything in list as verified by NSLog()?
    – zaph
    Nov 23, 2013 at 17:02
  • 1
    You'd need to actually accumulate all the records in order to write them all. Your list will only include one record. (And if you write multiple records there will be nothing to separate them. You need to re-think what you're trying to do.)
    – Hot Licks
    Nov 23, 2013 at 18:13

1 Answer 1

0

You need to create the NSMutableArray once and make all additions to it.

NSMutableArray *list = [[NSMutableArray alloc] init];
while (sqlite3_step(searchStatement) == SQLITE_ROW)
{
    NSString *nickname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 0)];
    NSString *time = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 1)];
    NSString *text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 2)];
    [list addObject:nickname];
    [list addObject:time];
    [list addObject:text];
    ...
}
[list writeToFile:filePATH atomically:YES];
3
  • Thanks... Your solution works fine! Can I write the file where there are the 3 elements in one row? Now the result is one element for row nickname 1 time 1 text 1 nickname 2 time 2 text 2 ... I wanna: nickname 1 time 1 text 1 nickname 2 time 2 text 2 .... It is possible? Nov 24, 2013 at 9:28
  • You could write code to create a new NSMutableArray, access the current array in the order you want and add the items to the new array. But the array as is is probable the best arraignment, just access as needed.
    – zaph
    Nov 24, 2013 at 12:41
  • Ask in another question. Also there are probably larger design issues that involve how this array will be used.
    – zaph
    Nov 24, 2013 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.