首页 > 解决方案 > 从 sqlite 获取数据并存储在 json 中

问题描述

我有一个表,我想代表特定列从中获取所有数据,然后想以 JSON 的形式保存该数据,以便我可以通过 API 将其发送到数据库进行保存。

尽管我尝试通过游标进行操作,但我无法从数据库中获取所有数据,但我在其中获取了单个数据。请帮助我获取数据并将其转换为 JSON。这是我编码的。这是Dbsave扩展类的方法DatabaseOpenHelper

public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+"autosave",null);
    return res;
}

然后我像这样在 Activity 中使用这种方法。

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();
  }
}

我只得到一个数据aaaa。然后我尝试这样做gettersetter但没有任何好处。

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();
}

我无法解析settergetter. 如果我能够获取所有数据,我将使用 GSON 将其转换为 JSON。

标签: javaandroidsqlitegsongetter-setter

解决方案


循环内部getalldata函数有问题。它不是在光标上迭代,而是一次又一次地循环相同的元素。我想建议更改如下功能。

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();
   }
}

希望能解决您的问题。

更新

要使用 GSON 将存储在ArrayListJSON 中的数据转换为 JSON,您需要首先在build.gradle文件中添加库。你可以在这里找到使用它的方法。

只需在 build.gradle 文件中添加以下依赖项。

dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

GSON 需要一个对象来将其转换为 JSON。因此,我建议您使用从光标中获取的元素创建一个对象,如下所示。

public class Data {
    public String aaa;
    public String bbb;
    public String ccc;
}

public class ListOfData {
    public List<Data> dataList;
}

现在再次修改函数,如下所示。

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();
   }
}

推荐阅读