user2149618
user2149618

Reputation: 75

query sqlite for specific column

i try to query for all data in specific colum but result in error "make sure that cursor initialized correctly before accessing thedata". is there any problem with this code thank you.

    public List<String> getAllItemName(){
    String[] column = new String[]{Pro_ID, Pro_Name, Pro_Price, Pro_Description, Pro_Date};
    Cursor c= ourDatabase.rawQuery("SELECT " + Pro_Name + " From "+ TABLE_NAME , null);

    List<String> lst = new ArrayList<String>();
     if (c.moveToFirst()) {
         do {
             lst.add(c.getString(1));
                              String name = c.getString(1);
                              Log.v(name,name + ("   name"));
         } while (c.moveToNext());
     }

     return lst;

}

Upvotes: 0

Views: 118

Answers (2)

s.d
s.d

Reputation: 29436

Cursor Indexes are 0 based. First column corresponds to :c.getString(0).

You can get index of a column using getColumnIndex(columnName)

Upvotes: 2

Anubhaw Arya
Anubhaw Arya

Reputation: 145

Try this instead of c.getString(1):

c.getString(c.getColumnIndex(Pro_Name))

Upvotes: 3

Related Questions