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

标签 java android sqlite gson getter-setter

我有一个表,我想从中获取代表特定列的所有数据,然后想以 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。

最佳答案

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

关于java - 从sqlite中获取数据并存储在json中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54643692/

相关文章:

c# - 检测何时在 Windows 中卸载应用程序

java - 将 Spring Security 与线程池结合使用会导致重用线程时出现竞争条件

android - 为什么我不能使用 adb 命令将 sqlite 推送到 Android?

c - 如何获取行数导致 SQL (C ANSI) 中的查询?

java - java 1.8 中的 jstack/jmap 替代方案

java - Teradata createClob : function not supported in this version. 这是错误还是功能?

java - 如何在同一行打印 2 个 for 循环的结果

java - 具有不可编辑消息正文的短信 Intent

android - 在 Android 中使用 FFmpeg 将视频裁剪为正方形格式

python - sqlite - 一次插入多个值