从 sqlite 中获取数据并存储在 json

Fetching data from sqlite and storing in json

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

虽然我尝试通过游标获取数据库中的所有数据,但我无法从数据库中获取所有数据,但我在其中获取的是单个数据。请帮助我获取数据并将其转换为 JSON。这就是我编写的代码。这是 Dbsave class 中的方法,它扩展了 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.

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 将存储在 ArrayList 中的数据转换为 JSON,您需要先在 build.gradle 文件中添加库。您可以找到使用它的方法 here

只需在您的 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();
   }
}

您每次在游标内都在初始化您的数组列表 在游标外初始化

public void getalldata(){
  cursor=dbAutoSave.getAllData();
 ArrayList<String> aaaa=new ArrayList<>();
  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);
          }


          aaaa.add(aaa);
          Toast.makeText(getApplicationContext(),"bbbbb"+aaaa,Toast.LENGTH_LONG).show();
      }
      cursor.close();
  }
}