【问题标题】:Storing an array into SQLite database将数组存储到 SQLite 数据库中
【发布时间】:2011-04-16 18:52:58
【问题描述】:

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

有没有什么方法可以直接将数组存入数据库??

下面是我的代码:

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

会追加工作吗?怎么样?

【问题讨论】:

    标签: objective-c ios arrays string sqlite


    【解决方案1】:

    根据this other post,SQLite 不直接支持数组。它只做整数、文本和浮点数。

    How to store array in one column in Sqlite3?

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

      • 不,它不工作......我认为它采用了一些垃圾值并将其打印出来
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 2014-04-07
      相关资源
      最近更新 更多