3

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

4
  • 1
    show ur db.getCartItems() you can make changes in query for specific columns you want tutorialspoint.com/sqlite/sqlite_select_query.htm Commented May 28, 2017 at 8:27
  • Oh yeah haha. I am an idiot, I can create another function to get the specific values I need haha. Thank you sir! You deserve an Upvote :) Commented May 28, 2017 at 8:30
  • 1
    You can also use if statements with continue to skip columns you selected if you don't wish to write a custom SELECT statement. Commented May 28, 2017 at 8:34
  • Well I got it to work already sir, I was just overcomplicating my problem haha. But thanks for the info sir, might check into that for extra knowledge :D Commented May 28, 2017 at 8:46

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help sir, my problem was so simple. I was just overcomplicating it haha.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.