有没有办法将 sqlite 转换为 json?所有其他问题都在解析 json 并保存到 sqlite。我似乎找不到任何关于此的引用,请帮助我。
我在应用程序中有一个 sqlite db,我需要将它转换为 json,升级 db 版本,解析之前转换的 json 并添加另一个表。关于我应该如何做到这一点的任何建议?
提前致谢。
最佳答案
引用 Link
private JSONArray getResults()
{
String myPath = DB_PATH + DB_NAME;// Set path to your database
String myTable = TABLE_NAME;//Set name of your table
//or you can use `context.getDatabasePath("my_db_test.db")`
SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
String searchQuery = "SELECT * FROM " + myTable;
Cursor cursor = myDataBase.rawQuery(searchQuery, null );
JSONArray resultSet = new JSONArray();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();
for( int i=0 ; i< totalColumn ; i++ )
{
if( cursor.getColumnName(i) != null )
{
try
{
if( cursor.getString(i) != null )
{
Log.d("TAG_NAME", cursor.getString(i) );
rowObject.put(cursor.getColumnName(i) , cursor.getString(i) );
}
else
{
rowObject.put( cursor.getColumnName(i) , "" );
}
}
catch( Exception e )
{
Log.d("TAG_NAME", e.getMessage() );
}
}
}
resultSet.put(rowObject);
cursor.moveToNext();
}
cursor.close();
Log.d("TAG_NAME", resultSet.toString() );
return resultSet;
}
关于android - 将 SQLite 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25722585/