SQLite的转换到JSON [英] Convert SQLite to JSON

查看:5832
本文介绍了SQLite的转换到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来转换源码为JSON?其他所有问题都是JSON解析并保存到SQLite的。我似乎无法找到任何这参考,请帮助我。

Is there a way to convert sqlite to json? All other questions are parsing json and saving to sqlite. I can't seem to find any reference on this, please help me.

我在应用程序中一个SQLite数据库,我需要它转换为JSON,升级版分贝,解析较早转换JSON并添加另一个表。我应该怎么做这个有什么建议?

I have a sqlite db inside the app and i need it to be converted to json, upgrade db version, parse earlier converted json and add another table. Any suggestions on how should I do this?

先谢谢了。

推荐答案

参考<一个href=\"http://tech.sarathdr.com/android-app/convert-database-cursor-result-to-json-array-android-app-development/\"相对=nofollow>链接

private JSONArray getResults()
{

String myPath = DB_PATH + DB_NAME;// Set path to your database 

String myTable = TABLE_NAME;//Set name of your table

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

这篇关于SQLite的转换到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆