0

I'm looking an efficient way to do this. A way that hopefully needs one string. I want the data presented like the following:

row1
row2
row3
row4

The reason I want to try and use one string is because I may call the same query from different tables and it will have a different number of rows. One might have 10, the other 8. So I can't specifically use 8 strings when there might be a table of 10.

Is there a way I can do it so that it simply collects one columns worth of data then I can format it to be like above/

What I have at the moment does it but uses multiple strings. See below:

//select stuff
    Cursor c = db.rawQuery("SELECT * FROM table_one", null);
        c.moveToFirst();
        in1 = c.getString(c.getColumnIndex("column_one"));
        c.moveToNext();
        in2 = c.getString(c.getColumnIndex("column_one"));
        c.moveToNext();
        in3 = c.getString(c.getColumnIndex("column_one"));
        c.moveToNext();
        in4 = c.getString(c.getColumnIndex("column_one"));
        c.moveToNext();
        in5 = c.getString(c.getColumnIndex("column_one"));

    String all = in1 + "\n" + in2 + "\n" + in3 + "\n" + in4 + "\n" + in5;

Of course this code assumes there will always be 5 bits of information in the column where as it varies.

Is there a better way?

Appreciate the help.

4

1 回答 1

0

You can use loop and construct data like this:

Cursor c = db.rawQuery("SELECT * FROM table_one", null);
String all = "";
if (c != null) {  
    if(c.moveToFirst())
        do {
            String in = c.getString(c.getColumnIndex("column_one"));
            all += in + "\n";
        } while (c.moveToNext());
    c.close();
} 
于 2013-10-23T18:35:38.790 回答