java - 从 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。
解决方案
循环内部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 中的数据转换为 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();
}
}
推荐阅读
- c++ - MiniDumpWriteDump() 进入命名管道,然后 ReadFile()
- google-api - 使用 gspread 或 gspread-formatting 批量检索工作表中所有单元格的“dataValidation”?
- javascript - 无法理解猫鼬虚拟中的“.this”
- java - 如何使用具有泛型和继承的方法?
- javascript - 使用 axios 渲染 ejs 时出现“无法读取数据属性错误”
- flutter - 将 Flutter 更新到 1.20.3-stable 后无法运行项目
- apache-kafka - 无法建立到节点 -1 的连接。经纪人可能不可用。Windows 和 WSL
- spring - Spring Security Roles Hierarchy Expression中的\是什么意思?
- mysql - MySQL - 根据团队成员返回多个用户
- java - 如何每个功能方法只执行一次设置方法/块?