Iterate through rows from Sqlite-query

后端 未结 7 1171
梦如初夏
梦如初夏 2020-12-02 17:57

I have a table layout that I want to populate with the result from a database query. I use a select all and the query returns four rows of data.

I use this code to

7条回答
  •  眼角桃花
    2020-12-02 18:47

    public void SQLfunction() {
        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    
        String[] sqlSelect = {"column1","column2" ...};
        String sqlTable = "TableName";
        String selection = "column1= ?"; //optional
        String[] selectionArgs = {Value}; //optional
    
        qb.setTables(sqlTable);
        final Cursor c = qb.query(db, sqlSelect, selection, selectionArgs, null, null, null);
    
       if(c !=null && c.moveToFirst()){
            do {
               //do operations
               // example : abcField.setText(c.getString(c.getColumnIndex("ColumnName")))
    
              }
            while (c.moveToNext());
        }
    
    
    }
    

    NOTE: to use SQLiteQueryBuilder() you need to add

    compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' in your grade file

提交回复
热议问题