Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Specific Columns from SQLite Database

Tags:

android

sqlite

I have an app that gets all the data from the sqlite database and converts it into JSON, but I was wondering how would I be able to do it if I only want to get the data of specific columns?

This is the code I use to get all the data from the SQLite databse and convert it to JSON:

  Cursor data = db.getCartItems();
        orderNameArray = new JSONArray();
        data.moveToFirst();
        while(data.isAfterLast() == false)
        {
            int totalColumn = data.getColumnCount();
            orderNameObject = new JSONObject();
            for (int i = 0; i < totalColumn; i++)
            {
                try {
                    orderNameObject.put(data.getColumnName(i), data.getString(i));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            orderNameArray.put(orderNameObject);
            data.moveToNext();
        }
        data.close();

Thanks in advance for all the insights or help in advance! :D

like image 518
Gionne Lapuz Avatar asked Feb 28 '26 05:02

Gionne Lapuz


1 Answers

If you want to fetch only selected fields of table, then use the following query:

SELECT Coulmn1, Coulmn2, Coulmn3 FROM TABLENAME;

or u want all data

SELECT * FROM TABLENAME;

for more info https://www.tutorialspoint.com/sqlite/sqlite_select_query.htm

like image 94
Pavan Avatar answered Mar 02 '26 13:03

Pavan



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!