Is it possible to insert a table of integers like int[] numbers
into an SQLite Table ?
How to create the column where I put this table?
this line of code:
values.put(MySqlHelper.COLUMN_CHILDS, numbers);
Returns me an error like:
Change type of `numbers` to `String`
SQLite arrays are not directly supported as a data type for columns in SQLite databases. In relational databases generally, an array is used to store rows with different key columns as an array dimension that means more than one key for a particular key. But in SQLite we cannot directly implement arrays in SQLite.
The INTEGER values in SQLite are stored in either 1, 2, 3, 4, 6, or 8 bytes of storage depending on the value of the number. REAL – this storage class is used to store the floating point values, and they are stored in an 8-bytes of storage.
First, connect to the SQLite database by creating a Connection object. Second, create a Cursor object by calling the cursor method of the Connection object. Third, execute an INSERT statement. If you want to pass arguments to the INSERT statement, you use the question mark (?) as the placeholder for each argument.
There is no array datatype in SQLite. You have two options:
Create 2 different tables, a "primary" table and a "numbers" table, where the numbers refer to a record (row) on the "primary" table via a foreign key.
(easier but not ideal): As @CloudyMarble suggests, Use Arrays.toString(numbers)
(or a StringBuilder
if for some reason that doesn't fit your needs) to put all of the numbers in your array into a comma-separated String and store that in your database. You can then pull that value back from the database into a Java array like so:
String[] s = stringFromDB.split(",");
int[] numbers = new int[s.length];
for (int curr = 0; curr < s.length; curr++)
numbers[curr] = Integer.parseInt(s[curr]);
insert your numbers array as a string:
Arrays.toString(numbers);
And on reading split the array using the split method and convert elements to integer as @drewmore's answer show.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With