user688175
user688175

Reputation: 29

Storing an array into SQLite database

I have 2 columns of type VARCHAR(1000) and type TEXT in a SQLite db. My requirement is to store the values in an array into the db. I tried storing the values in the array into a string using the for loop but once the control comes out of the for loop the values displayed for the string is only the last element in the array...

Is there any way using which I can directly store the array into the DB??

Below is my code:

NSString *tempVal=0, *tempFlag=0;
NSString *temp[256], *Flag[256];

    for(int i=0; i<256; i++)
    {
    NSLog(@"HELLO %d", tempVal);
        temp[i] = (NSString*)valMines[i];
        Flag[i] = (NSString*)openFlag[i];

        NSLog(@"ValMines is %d", temp[i]);
        NSLog(@"OpenFlag is %d", Flag[i]);

        tempVal = temp[i];
        tempFlag == Flag[i];

        NSLog(@"ValMines is %d %d", temp[i], tempVal);
    }

NSLog(@"Inside Save Data func");
NSLog(@"ValMines is %d", tempVal);
NSLog(@"OpenFlag is %d", tempFlag);

Would append work on it? How?

Upvotes: 2

Views: 6789

Answers (2)

Edward Castano
Edward Castano

Reputation: 617

According to this other post, SQLite does not support arrays directly. It only does ints, text and floats.

How to store array in one column in Sqlite3?

Upvotes: 1

Black Frog
Black Frog

Reputation: 11713

Try this:

NSMutableString *tempVal = [NSMutableString stringWithCapacity:1000];
NSMutableString *tempFlag = [NSMutableString stringWithCapacity:256];

for (int i = 0; i < 256; i++)
{      
    NSLog(@"ValMines is %@", valMines[i]);
    NSLog(@"OpenFlag is %@", openFlag[i]);

    // adding a space between each string
    [tempVal appendFormat:@" %@", valMines[i]];
    [tempFlag appendFormat:@" %@", openFlag[i]];
}

NSLog(@"Inside Save Data func");
NSLog(@"ValMines is %@", tempVal);
NSLog(@"OpenFlag is %@", tempFlag);

Upvotes: 0

Related Questions