Sorin Grecu
Sorin Grecu

Reputation: 1034

Sqlite columns to string array

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

Upvotes: 0

Views: 1901

Answers (1)

droideckar
droideckar

Reputation: 1067

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).

Upvotes: 2

Related Questions