Search code examples
android-sqlite

reading the date from sqlite in android


I am storing some date in sqlite and when I try to retrieve data, it is returning null.

private ArrayList<String> file_list = new ArrayList<>();

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         ArrayList<String> _list = db.get_file
              (String.valueOf(myFiles.get(position).getId()), file_list);
         Log.e("TAG","_list " + _list);

         case R.id.get_file:
            boolean save_file = db.add_file(String.valueOf(myFiles.get(position).getId()),
                                 file_list);
                    if (save_file){
                        Toast.makeText(this, "saved successfully", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "saved failed", Toast.LENGTH_SHORT).show();
                    }

    public ArrayList<String> get_file(String id, ArrayList<String> arrayList){

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT " + COL_FILE + " FROM "
            +TABLE_NAME+" WHERE " + _ID +" =? " , new String[] {String.valueOf(id)});

    if(cursor.moveToFirst()){
        return arrayList;
    } else {
        return null;
    }
}

 public TrackGroupArray read_tga_file(String id, TrackGroupArray tga)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT " + TGA_FILE + " FROM "
                +TABLE_NAME+" WHERE " + _ID +" =? " , new String[] {String.valueOf(id)});

        if(cursor.moveToFirst()){
            return tga;
        } else {
            return null;
        }
    }
  

   TrackGroupArray read_tga = db.read_tga_file(String.valueOf(myFiles.get(position).getId()),
                    trackGroupArray);

     

Every time I save a file, it is saved successfully, I have checked in the db and the file is there. But when I want to retrieve it, Log.e("TAG","_list " + _list); returns null.


Solution

  • Your get_File method, even if there is a row and the moveToFirst succeeds, will always return null as in the event that a row is located/selected you do nothing other than return an un-instantiated arrayList.

    You need to

    • a) instantiate the arrayList (otherwise it will be null) and then
    • b) add elements to the arrayList for each row that exists if any.

    So something like :-

       @SuppressLint("Range")
       public ArrayList<String> get_file(String id, ArrayList<String> arrayList) {
    
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor cursor = db.rawQuery("SELECT " + COL_FILE + " FROM "
                  + TABLE_NAME + " WHERE " + _ID + " =? ", new String[]{String.valueOf(id)});
    
          ArrayList<String> rv = new ArrayList<>(); //<<<<< A instantiate the ArrayList<String>
          
          /* Loop through the returned row(s) if any */
          while (cursor.moveToNext()) {
             /* for each iteration add an element to rv (the ArrayList) */
             rv.add(cursor.getString(cursor.getColumnIndex(COL_FILE)));
          }
          cursor.close(); //<<<<< should ALWAYS close Cursors when done with them
          return rv; // return the arraylist
       }