pushpa
pushpa

Reputation: 573

How to fetch the data from the different tables in android?

My application has multiple SQLite tables(around 20 with different fields). This I have created it using Adapter class(Here i have just mentioned one class Game, Like that i have 20).

public class MyDbAdapter {
private SQLiteDatabase mDb;
private static MyDatabaseManager mDbManager;
Context ctx;

public MyDbAdapter(Context context) {
       this.ctx =   context;
    mDbManager = new MyDatabaseManager(context);
    mDb = mDbManager.getWritableDatabase();
}

public static final class GameColumns implements BaseColumns {
    public static final String TABLE = "game";
    public static final String IMEI = "imei";
    public static final String LAST_UPDATE = "lastupdate";
    public static final String NICKNAME = "nickname";
}



private static class MyDatabaseManager extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "dbname";
    private static final int DATABASE_VERSION = 8;

    private MyDatabaseManager(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createGameTable(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

       db.execSQL("DROP TABLE IF EXISTS "+GameColumns.TABLE);
        onCreate(db);

    }

    @SuppressWarnings("unused")
    private void dropDatabase(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS " + GameColumns.TABLE);
    }

    private void createGameTable(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + GameColumns.TABLE + " ("
                + GameColumns._ID + " INTEGER PRIMARY KEY,"
                + GameColumns.IMEI + " TEXT,"
                + GameColumns.LAST_UPDATE + " TEXT,"
                + GameColumns.NICKNAME + " TEXT);");

      }
  }
 }

Data From each table is fetching using below code. This function returns Cursor object.

public Cursor getRecords(String tableName){
    Cursor cursor   = mDb.query(tableName, null, null, null, null, null, null);
    return  cursor;
}

I need to convert Cursor Object to either list or string. For this we need to iterate the cursor Object. My problem is, each table has different fields, So i need to iterate each cursor object in separate method. How can this be done using single method? Is there any direct way to convert cursor to string?

Upvotes: 0

Views: 1020

Answers (3)

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

you can use HashMap like...

ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();

int col_count = cursor.getColumnCount();
                    while (cursor.moveToNext()) {
                        HashMap<String, String> currentItem = new HashMap<String, String>();
                        for (int i = 0; i < col_count; i++)

                        {
                            String col_name = cursor.getColumnName(i);
                            currentItem.put(col_name, cursor.getString(i));
                        }
                        items.add(currentItem);
                    }
                    cursor.close();

Upvotes: 1

Soumyadip Das
Soumyadip Das

Reputation: 1791

try something like this:

ArrayList<Object> attayList;

if(tableName.equals(table1)){
arrayList=new ArrayList<Table1ClassType>();
}
if(tableName.equals(table2){
arrayList = new ArrayList<Table2ClassType>();
}

the main moto is creating new instance based on data, so you may check the table type & create new instance, then use arrayList easily.

Upvotes: 0

Egor
Egor

Reputation: 40203

You can pass a String array containing table column names as an argument to your getRecords() method. Then just iterate over the Cursor fetching the values for each column. Hope this helps.

Upvotes: 0

Related Questions