将数组存储到SQLite数据库中

时间:2011-04-16 18:52:58

标签: objective-c ios arrays string sqlite

我有两列类型为VARCHAR(1000)并在SQLite数据库中输入TEXT。我的要求是将数组中的值存储到db中。我尝试使用for循环将数组中的值存储到字符串中,但是一旦控件出现在for循环中,字符串显示的值只是数组中的最后一个元素...

有没有办法可以直接将数组存储到DB ??

以下是我的代码:

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

会附加工作吗?怎么样?

2 个答案:

答案 0 :(得分:1)

根据这篇文章,SQLite不直接支持数组。它只有整数,文本和浮点数。

How to store array in one column in Sqlite3?

答案 1 :(得分:0)

试试这个:

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