【发布时间】:2014-12-29 21:50:46
【问题描述】:
我在我的 android 应用程序的 SQlite 中存储了一些数据,我想将其导出为 json 文件并通过 post HTTP 将其发送到服务器。 您实际上可以在此链接中看到我的代码 - Old question containing my code
在网上搜索后,我只找到了相反的示例(从 json 到 sqlite)。我很乐意得到一些帮助/推动正确的方向。
编辑 -
我在尝试初始化 Gson 时遇到了一个小异常 - String json = gson.toJson(Record); .你能看一下吗 -
public ArrayList<Record> getAllRecordsArray() {
ArrayList<Record> localList = new ArrayList<Record>();
String selectQuery = "SELECT * FROM " + TABLE_RECORD;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//Loops through all rows and adds them to the local list
if(cursor.moveToFirst())
{
do {
//Get record information
Record record = new Record();
record.setpLat(cursor.getDouble(0));
record.setpLong(cursor.getDouble(1));
record.setpAcc(cursor.getFloat(2));
record.setpTime(cursor.getLong(2));
//Add record to list
localList.add(record);
} while (cursor.moveToNext());
}
return localList;
}
Gson gson = new Gson();
String json = gson.toJson(Record);
【问题讨论】:
-
谢谢。处理 myPath=DB_PATH 时,如何知道我的数据库路径?
-
创建数据库的路径。默认情况下,它类似于
/data/data/com.me.myapp/databases/mydb.db或者您可以使用String dbname = "mydb.db"; Path dbpath = ctx.getDatabasePath(dbname);