Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a table of integers - int[] - into SQLite database,

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`
like image 741
Hossam Oukli Avatar asked May 14 '13 10:05

Hossam Oukli


People also ask

Can you store an array in SQLite?

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.

How does SQLite store integers?

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.

How do you insert data into a SQLite table in Python?

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.


2 Answers

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]);
like image 179
drew moore Avatar answered Oct 08 '22 06:10

drew moore


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.

like image 27
CloudyMarble Avatar answered Oct 08 '22 04:10

CloudyMarble