Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving column names with column values in sqlite using cursor

I want to display column names with column values in the Listview using cursor. My present code shows only the column values.

public void openAndQueryDatabase() {

    db = openOrCreateDatabase( "mydatabase.db", SQLiteDatabase.CREATE_IF_NECESSARY , null ); 

    Cursor cursor = db.rawQuery("select * from "+ table + " where name='"+ name + "'",  null);


    int count = cursor.getColumnCount();

    if (cursor!=null )
    { 

        if  (cursor.moveToFirst())
        {
            do 
            {
                 for (int i =0 ; i< count; i++)
                {
                String data = cursor.getString(i);
                details.add(data);
                }
            } 
            while (cursor.moveToNext()); 
        } 

         }
}
like image 759
PrachiN Avatar asked Oct 20 '25 04:10

PrachiN


2 Answers

In this case you should use hashmap

for (int i =0 ; i< count; i++)
{
    String data = cursor.getString(i);
    String column_name = cursor.getColumnName(i);

    HashMap<String,String> map = new HashMap<String,String>();
    map.put("column_value",data);
    map.put("column_name",column_name);

    details.add(map); //change the type of details from ArrayList<String> to arrayList<HashMap<String,String>>
}
like image 92
Sunny Garg Avatar answered Oct 22 '25 19:10

Sunny Garg


Use getColumnName(int columnIndex) to get the name of the row for a given index.

like image 33
Gabe Sechan Avatar answered Oct 22 '25 18:10

Gabe Sechan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!