首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在安卓系统中将ArrayList保存在SQLite数据库中

在安卓系统中将ArrayList保存在SQLite数据库中
EN

Stack Overflow用户
提问于 2010-06-29 23:39:14
回答 6查看 55.6K关注 0票数 23

我一直在使用android上的SQLite,我想将数组列表添加到表的列中,然后以数组列表的形式取回数据。arraylist是Long的列表。我注意到SQL有一个用于存储blob的选项,但是在能够将其作为blob存储在我的SQLite数据库中之前,我看起来需要首先将arraylist转换为byte[]。

如果任何人对如何将数组列表保存到SQLite数据库中有一个解决方案,将不胜感激。或者,我应该考虑是否有其他选择来保存我的数据数组?

EN

回答 6

Stack Overflow用户

发布于 2010-06-30 01:35:11

请原谅我粗暴地抄袭了我之前对BLOB vs. VARCHAR for storing arrays in a MySQL table的回答。那里的其他答案也非常中肯。

我认为Con的方法可能比使用java序列化更好,因为java的内置序列化将需要额外的字节,并且非java应用程序将更难处理数据。

代码语言:javascript
复制
public static void storeInDB(ArrayList<Long> longs) throws IOException, SQLException {

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(bout);
    for (long l : longs) {
        dout.writeLong(l);
    }
    dout.close();
    byte[] asBytes = bout.toByteArray();

    PreparedStatement stmt = null;  // however you get this...
    stmt.setBytes(1, asBytes);
    stmt.executeUpdate();
    stmt.close();
}

public static ArrayList<Long> readFromDB() throws IOException, SQLException {

    ArrayList<Long> longs = new ArrayList<Long>();
    ResultSet rs = null;  // however you get this...
    while (rs.next()) {
        byte[] asBytes = rs.getBytes("myLongs");
        ByteArrayInputStream bin = new ByteArrayInputStream(asBytes);
        DataInputStream din = new DataInputStream(bin);
        for (int i = 0; i < asBytes.length/8; i++) {
            longs.add(din.readLong());
        }
        return longs;
    }

}

注意:如果您的列表有时包含超过31个长度(248字节),那么您将需要使用BLOB。不能在MySQL中使用BINARY()或VARBINARY()。我知道你问的是SQLite,但本着完全抄袭我之前的回答的精神,我会假装你问的是MySQL:

代码语言:javascript
复制
mysql> CREATE TABLE t (a VARBINARY(2400)) ;
ERROR 1074 (42000): Column length too big for column 'a' (max = 255);
use BLOB or TEXT instead
票数 7
EN

Stack Overflow用户

发布于 2012-10-31 09:58:32

我有两个ArrayList<String>,都是will 1000+条目。我研究了blobs和bytes,但对我来说,加快进程并使其可用的解决方案是通过更改insert方法并消除database.insert - Credit,这就是here

代码语言:javascript
复制
private static final String INSERT = "insert into "
            + YOUR_TABLE_NAME+ " (" + COLUMN_1 + ", "
            + COLUMN_2 + ") values (?, ?)";

public void insertArrayData(ArrayList<String> array1,
            ArrayList<String> array2) {

        try {
            database.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        int aSize = array1.size();

        database.beginTransaction();

        try {
            SQLiteStatement insert = database.compileStatement(INSERT);

            for (int i = 0; i < aSize; i++) {

                insert.bindString(1, array1.get(i));
                insert.bindString(2, array2.get(i));
                insert.executeInsert();

            }

            database.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
        }

        try {
            database.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

它很容易适应长度和整数等,照明速度快。因此,谢天谢地,我不必再为blob和字节而挠头了!希望能有所帮助。

票数 6
EN

Stack Overflow用户

发布于 2016-01-13 21:08:59

有一种更简单的方法,可以用完全不同的方式来做这件事。您可以生成一个由所有数组值组成的字符串。为此,创建一个StringBuilder并连续追加这些值,并使用分隔符(就像不会在数组值中使用的简单分隔符)。例如,虚拟'|‘。例如,在代码中:

代码语言:javascript
复制
double[] mylist = new double[]{23, 554, 55};
    StringBuilder str = null;
    str = new StringBuilder();

    for(int i=0;i<mylist.length;i++){
        str.append(mylist[i]+"|");
    }

    String result = str.toString();

    db.open();
    db.insert(result);
    db.close();

当您想要获取它们并使用这些值时。从数据库中获取列,将其转换为字符串,并使用splite()运算将数组中的每个值都放在一个数组列中,这样您就可以轻松地使用它了:)

让我们用代码来实现:

代码语言:javascript
复制
String str = db.getdata();
    String[] list = str.split("|");

通过一个简单的转换,你可以使用它们作为双倍;

代码语言:javascript
复制
double mydouble = Double.parsDouble(list[1].toString());

也许它不是标准的,但它是有帮助的,希望能有所帮助;)

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3142285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档