How to store array in one column in Sqlite3?

后端 未结 4 1008
日久生厌
日久生厌 2020-12-09 14:44

Is there any way to store an array of integers in one column of table? I want o/p like this:

ident | value                                                            


        
4条回答
  •  情深已故
    2020-12-09 15:25

    Sorry to Necro, just came across the problem myself and found a solution.

    As stated already SQLite has no support for arrays, so you can't store them as such. Try this, I had the same problem;

    Instead of storing array elements individually, you can store them as a large string and use a string function or Regular expression to parse them back into their types. An C# example

    int[] myArray = new int[] {8,4,345,378,34456,7};
    
    string Arraystring = myArray[0].ToString();
    
    for(int i = 1; i < myArray.Length; i++) { 
    Arraystring += "," + myArray[i].ToString();
    
    }
    

    This will turn the array into a single string, now we take the string and insert it into the table as a string, when you read the string use this code to get the array back. Another C# example

    string value; //assign this via Reader
    string[] tokens = values.Split(',');
    
    int[] myItems = Array.ConvertAll(tokens, int.Parse);
    

    this will only work with single dimensional arrays, multi-dimensional can get tricky when it comes to parsing the strings.

提交回复
热议问题