SQLite
 sql >> Teknologi Basis Data >  >> RDS >> SQLite

Konversikan SQLite ke JSON

Tautan Referensi

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


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. 2 Cara Mengaktifkan Bungkus Kata di SQLite

  2. Cara Mendapatkan Tanggal Saat Ini di SQLite

  3. Praktik terbaik untuk bekerja dengan banyak tabel

  4. nullvalue:Tampilkan Nilai NULL di SQLite

  5. java.lang.IllegalStateException:Tidak dapat membaca baris 0, col -1 dari CursorWindow - Masalah sqlite Android