开发者

Storing an array into SQLite database

开发者 https://www.devze.com 2023-02-25 20:00 出处:网络
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

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?


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?


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);
0

精彩评论

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

关注公众号