lundi 2 novembre 2015

How Do I Read SQLite Data in Activity

I am reading data from SQLite Database using below code in DatabaseHandler Class

public String[] getAllData() {

        String selectQuery = "SELECT id FROM " + TABLE_USER;
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery(selectQuery, null);
                 String[] data = null;

        if (cursor.moveToFirst()) {
            do {

                int id = cursor.getInt(cursor.getColumnIndex("id"));
                Log.d("TAG","id:-"+id);   

                String select = "SELECT name FROM " + TABLE_JOBS+ " where userid =" +id;         

                Cursor c = db.rawQuery(select, null);              

                if (c.moveToFirst()) {
                    do {

                        String rname = c.getString(c.getColumnIndex("name"));                       
                        Log.d("TAG","name:-"+name);                    

                    } while (c.moveToNext());
                }       


            } while (cursor.moveToNext());
        }

        db.close();
        return data;

    }

MainActivity.java:

DatabaseHandler dh;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

            dh = new DatabaseHandler(MainActivity.this);

            buttonGetData.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dh.getAllData();
                }
            });

   }

QUESTION

 How can I read same data in Activity ?

Where I have to make change? What I need to use to get it done ?

I am not placing complete DatabaseHandler code, if you need that to resolve this issue, then let me so I will post that too..

Aucun commentaire:

Enregistrer un commentaire