English 中文(简体)
Saving ArrayList in Profite database in Anders
原标题:Saving ArrayList in SQLite database in Android

我先与Kallite公司合作,我想在表格一栏中增加一个阵列清单,然后把数据作为阵列清单。 阵列清单是长者名单。 我已注意到,SQ有一个储存BLOBS的选择,但我认为,首先需要将阵列清单改成一个星号,然后才能将其储存在我的SQite数据库中。

如果有人能找到解决办法,如何将阵列清单储存到一个可得到极大赞赏的数据库中。 或者是否有其他选择可以拯救我的系列数据,即应考虑吗?

问题回答

插入:

ArrayList<String> inputArray=new ArrayList<String>();

/...。 投入的价值

Gson gson = new Gson();

String inputString= gson.toJson(inputArray);

System.out.println("inputString= " + inputString);

利用“投入String”来节省Lum数据库中的ArrayList的价值

反弹:

Get the String from the SQLiteDatabse what you saved and changed into ArrayList type like below: outputarray is a String which is get from SQLiteDatabase for this example.

Type type = new TypeToken<ArrayList<String>>() {}.getType();

ArrayList<String> finalOutputString = gson.fromJson(outputarray, type);
  • In SQLite use text as format to store the string Value.....

请原谅我原封不动地想象一下我先前对

我认为,科恩办法可能比采用java序列化办法好,因为java的序列化将需要更多的用药,而非java的申请处理数据的时间会更长。

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中使用双语或双语。 我认识到你再次问道,但本着完全想象我先前的回答的精神,我将恳请大家问我的我的我的我的我的我的我的我的我的我的我的我的我的我的我的我的我的回答:

mysql> CREATE TABLE t (a VARBINARY(2400)) ;
ERROR 1074 (42000): Column length too big for column  a  (max = 255);
use BLOB or TEXT instead

我有2个<代码>ArrayList<String>,这两个条目都将是1 000多个。 我看望博彩和tes,但对于我来说,通过改变插入方法和删除<代码>数据库,加快进程并使之可行的解决办法。 - “http://sagistech.blogspot.co.uk/2010/07/notes-on-android-sqlite-bukl-insert.html”

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();
        }
    }

它易于适应长老和低温等,快速缓解。 因此,我很感谢的是,我竟然要把我的头部推倒在花旗上。 希望会有所助益。

There is an easier way that do such thing in completely another way. you can make an string that consists of all your array values. for that make an StringBuilder and append the values continuously and offcource with a separator (like a simbole you which you won t use in your array values . for example virtual | . in code for example :

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();

当你想 them取这些价值观时。 从数据库中取出一栏,纯粹从数据库到Sting,而单列阵列中每个阵列数值的相左,可以轻易使用:

引文:

String str = db.getdata();
    String[] list = str.split("|");

简单转换后,你可以将其用作双倍;

double mydouble = Double.parsDouble(list[1].toString());

也许这不是标准,而是有帮助的,希望有帮助。

你们不得不人工操作,处理名单上的每一项目,并在数据库中储存之前加以改动。

for (long l : array<long>){
//change to byte here
//Store in database here
}
    ArrayList<String> list = new ArrayList<String>();
                    list.add("Item 1");
                    list.add("Item 2");
                    list.add("Item 3");

                    String joined = TextUtils.join(",", list);
                    Log.i(TAG, "joined strings: " + joined);

                    String[] array = TextUtils.split(joined, ",");
                    Log.i(TAG, "joined strings: " + array[0] + array[1] + array[2]);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签