Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert SQLite to JSON

Tags:

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.

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?

Thanks in advance.

like image 511
Lendl Leyba Avatar asked Sep 08 '14 10:09

Lendl Leyba


People also ask

Can SQLite save JSON?

SQLite stores JSON as ordinary text. Backwards compatibility constraints mean that SQLite is only able to store values that are NULL, integers, floating-point numbers, text, and BLOBs. It is not possible to add a sixth "JSON" type.

Does SQLite return JSON?

It's possible to output query results as a JSON document when using the SQLite command line interface. We can do this with the json output mode. We can also use SQLite functions like json_object() and/or json_array() to return query results as a JSON document.

Can you convert a SQLite database to MySQL?

The quickest and easiest way to convert SQLite to MySQL is by exporting an SQL Database to a Dump File, and then importing the SQLite Dump into MySQL Database. You can export an SQLite Database to Dump File using the . dump command.


2 Answers

Reference 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;   } 
like image 130
Sagar Pilkhwal Avatar answered Oct 17 '22 10:10

Sagar Pilkhwal


Quick and easy way if you don't feel like coding it:

  • Download DB Browser for SQLite: https://sqlitebrowser.org/ (yes, it's free)
  • Open the SQLite DB
  • Go to File > Export > Table(s) to JSON
  • Voila

Beware, for some reason it does not correctly convert NULL values. It converts this to an empty string... Besides that it works like a charm as far as I now.

like image 24
kristofvdj88 Avatar answered Oct 17 '22 11:10

kristofvdj88