0

How can i put every column from an android sqlite database into a different string array ? I have a database with 4 columns :id,one in which i store the date,another for the hour and another for a REAL value. What i need is to fetch the hour and REAL value columns by the date and put the hours column in one string[] and the REAL values column in another string[]. I will then need to put each of the two string arrays in two different layouts.So it is not necessary to be string arrays,it can be anything else as long as it suits my needs.

Currently i'm using this but i don't know how to put it into string arrays further

public Cursor fetchCountriesByName(String inputText) throws SQLException {
    Cursor mCursor = null;
    if (inputText == null || inputText.length() == 0) {
        mCursor = this.sqliteDBInstance3.query(DB_TABLE_NAME3, new String[] {
                DB_COLUMN_1_NAME3, DB_COLUMN_2_NAME3
                }, null, null, null, null, null);

    } else {
        mCursor = this.sqliteDBInstance3.query(true, DB_TABLE_NAME3,
                new String[] { DB_COLUMN_1_NAME3,
                        DB_COLUMN_2_NAME3 },
                DB_COLUMN_3_NAME3 + " like '%" + inputText + "%'", null,
                null, null, null, null);
    }
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

This is my table creation :

private static final String DB_NAME3 = "usingsqlite3.db";
private static final int DB_VERSION_NUMBER3 = 1;
private static final String DB_TABLE_NAME3 = "countries3";
private static final String DB_COLUMN_1_NAME3 = "country_value3";
private static final String DB_COLUMN_2_NAME3 = "country_hour";
private static final String DB_COLUMN_3_NAME3 = "country_date";


private static final String DB_CREATE_SCRIPT3 = "create table " + DB_TABLE_NAME3 +
                        " (id3 integer primary key autoincrement, country_value3 REAL, country_hour TEXT, country_date TEXT);)";
0

1 Answer 1

2

you can use an ArrayList to do this, for example:

Cursor c = fetchCountriesByName("someCountry");
ArrayList<String> data = new ArrayList<String>();
while (c.moveToNext()) {
    data.add(c.getString(DB_COLUMN_1_NAME3));
}

and then you can convert the array list into a string array (search on how to do this).

Sign up to request clarification or add additional context in comments.

2 Comments

Will this work with 2 columns/ArrayLists ? I need to get two columns from the table,the one with the hour and the one with the value.
sure, you can just add another arraylist and add the 2nd column data to that: data2 = new ArrayList<String>(); then, data2.add(c.getString(DB_COLUMN_2_NAME3), etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.