将整数表-int []-插入SQLite数据库, [英] Insert a table of integers - int[] - into SQLite database,
问题描述
是否可以将整数表(例如 int []数字
)插入到SQLite表中?
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?
此行代码:
values.put(MySqlHelper.COLUMN_CHILDS, numbers);
向我返回错误,例如:
Change type of `numbers` to `String`
推荐答案
SQLite中没有数组数据类型。您有两个选择:
There is no array datatype in SQLite. You have two options:
创建2个不同的表,一个主表和一个数字表,其中数字指的是主表通过外键。
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.
(更简单但不理想):如@CloudyMarble所建议,使用 Arrays.toString(numbers)
(或 StringBuilder
(如果由于某些原因不符合您的需要),将数组中的所有数字放入逗号分隔的String中,并将其存储在数据库中。然后,您可以像这样将值从数据库中拉回Java数组:
(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]);
这篇关于将整数表-int []-插入SQLite数据库,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!