I have a table from which I want to fetch all data on behalf of a specific column and then want to save that data in form of JSON so that I can send it over API to the database for saving.
I am unable to get all data from the database though I tried doing it through cursor I am getting single data in this. Please help me in fetching the data and converting it into JSON. This is what I have coded. This is the method from Dbsave
class which extends DatabaseOpenHelper
.
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+"autosave",null);
return res;
}
And then I am using this method in the Activity like this.
public void getalldata(){
cursor=dbAutoSave.getAllData();
if (cursor!=null) {
if(cursor.moveToNext()) {
for (int i = 0; i <= cursor.getCount(); i++) {
aaa = cursor.getString(1);
String bbb = cursor.getString(2);
String ccc = cursor.getColumnName(3);
}
ArrayList<String> aaaa=new ArrayList<>();
aaaa.add(aaa);
Toast.makeText(getApplicationContext(),"bbbbb"+aaaa,Toast.LENGTH_LONG).show();
}
cursor.close();
}
}
I am getting only one data in aaaa
. Then I tried doing this with gettersetter
but with no benefit.
private void showEmployeesFromDatabase() {
Cursor cursorEmployees = mDatabase.rawQuery("SELECT * FROM autosave", null);
if (cursorEmployees.moveToFirst()) {
do {
// Pushing each record in the employee list
employeeList.add(new SetterGetter(
cursorEmployees.getString(0),
cursorEmployees.getString(1),
cursorEmployees.getString(2)
));
} while (cursorEmployees.moveToNext());
}
// Closing the cursor
System.out.println("aaaaaa" + employeeList.get(1));
cursorEmployees.close();
}
I am unable to parse the data from the list in settergetter
. If I will be able to fetch all data, I will use GSON to convert it into JSON.
The loop inside getalldata
function is faulty. It's not iterating over the cursor and just looping over the same element again and again. I would like to suggest to change the function like the following.
public void getalldata() {
// Cursor is loaded with data
cursor = dbAutoSave.getAllData();
ArrayList<String> aaaa = new ArrayList<>();
if (cursor != null) {
cursor.moveToFirst();
do {
aaa = cursor.getString(1);
String bbb = cursor.getString(2);
String ccc = cursor.getColumnName(3);
// Add into the ArrayList here
aaaa.add(aaa);
} while (cursor.moveToNext());
cursor.close();
}
}
Hope that fixes your problem.
Update
To convert the data stored in the ArrayList
to JSON using GSON, you need to add the library first in your build.gradle
file. You can find a way of using it here.
Just add the following dependency in your build.gradle file.
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}
GSON takes an object for converting it to JSON. So I would like to suggest you create an object with the elements fetched from your cursor like the following.
public class Data {
public String aaa;
public String bbb;
public String ccc;
}
public class ListOfData {
public List<Data> dataList;
}
Now modify the function again like the following.
public void getalldata() {
// Cursor is loaded with data
cursor = dbAutoSave.getAllData();
ArrayList<Data> dataList = new ArrayList<Data>();
if (cursor != null) {
cursor.moveToFirst();
do {
Data data = new Data();
data.aaa = cursor.getString(1);
data.bbb = cursor.getString(2);
data.ccc = cursor.getColumnName(3);
// Add into the ArrayList here
dataList.add(data);
} while (cursor.moveToNext());
// Now create the object to be passed to GSON
DataList listOfData = new DataList();
listOfData.dataList = dataList;
Gson gson = new Gson();
String jsonInString = gson.toJson(listOfData); // Here you go!
cursor.close();
}
}
I was wondering if someone could help append data to an XML file. Below, I have code where it grabs data from another xml file, does some logic changing, and writes the changes to a new output file. ...
Is it possible to identify, probably using reflection, whether some method within a specific java class calls another method at build time? Let's say that I have ClassA that declares methodA1() ...
I am using Apache Active MQ with Spring..... the problem I am facing is that I am creating producer on one machine let say Machine1 and I am creating one consumer on second machine let say Machine2... ...
I am trying to pass dimensions to a Java Swing JPanel creator class. No luck so far. I even put the contentPane.setPreferredSize(new Dimension(intWidth, intHeight)); in a try / catch block and I did ...