Problem with SQLite DB

IdoMajor41

New Member
I have done this code :
B4X:
public class SQLiteDB {

   
   String TAG = SQLiteDB.class.getSimpleName();
   
   public static final String key_id = "id";
   public static final String key_playlistName = "Playlist";
   public static final String key_songname = "SongName";

   
   private static final String TABLE_NAME = "playlists";
   private static final String DB_NAME = "Medio";
   private static final int DB_VERSION = 1;
   
   private Context context;
   String sql= String.format(" create table %s ( %s INTEGER PRIMARY KEY , %s TEXT NOT NULL," +
         " %s TEXT NOT NULL)", TABLE_NAME, key_id, key_playlistName,key_songname);
   private DBHelper dbHelper;
   private SQLiteDatabase db;

   public SQLiteDB(Context c)
   {
      this.context=c;
   }
   public SQLiteDB openDB()
   {
      dbHelper = new DBHelper(this.context);
      db = dbHelper.getWritableDatabase();
      return this;
   }
   public void close()
   {
      db.close();
      dbHelper.close();
   }
   public ContentValues createEntry(int id , String playListName,String songName)
   {
      ContentValues cv = new ContentValues();
      cv.put(key_id, id);
      cv.put(key_playlistName, playListName);
      cv.put(key_songname, songName);
      return cv;
   }
   public SQLiteDB createEntries(Vector<ContentValues> contents)
   {
      for(int i=0;i<contents.size();i++)
      {
         db.insert(TABLE_NAME, null, contents.get(i));
      }
      return this;
   }
   private class DBHelper extends SQLiteOpenHelper
   {

      Context c;
      public DBHelper(Context context) 
      {
         super(context, DB_NAME, null, DB_VERSION);
         // TODO Auto-generated constructor stub
         this.c=context;
         Log.d(TAG, "DBHelper");
      }

      @Override
      public void onCreate(SQLiteDatabase db) {
         // TODO Auto-generated method stub
         Log.d(TAG, "onCreate"+sql);
         db.execSQL(sql);
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         // TODO Auto-generated method stub
         db.execSQL("drop table if exists"+ TABLE_NAME);
         Log.d(TAG,"onUpgrade dropped table"+TABLE_NAME);
         onCreate(db);
      }
   }
   public List<String> readPlaylists()
   {
      List<String> playlists = new ArrayList<String>();
      playlists.add("Add Playlist...");
      String [] columns = new String[]{key_id,key_playlistName,key_songname};
      Cursor c = this.db.query(TABLE_NAME, columns, null, null, null, null, null);
      int iRow = c.getColumnIndex(key_id);
      int iPlayListName = c.getColumnIndex(key_playlistName);
      int iSongName = c.getColumnIndex(key_songname);
      for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
      {
         if(!playlists.contains(c.getString(iPlayListName)))
            playlists.add(c.getString(iPlayListName));
      }
      return playlists;
   }

   public List<String> readData(String playlistname) {
      List<String> songs = new ArrayList<String>(); 
      String [] columns = new String[]{key_id,key_playlistName,key_songname};
      Cursor c = this.db.query(TABLE_NAME, columns,null, null, null, null, null);
      int iRow = c.getColumnIndex(key_id);
      int iPlayListName = c.getColumnIndex(key_playlistName);
      int iSongName = c.getColumnIndex(key_songname);
      for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
      {
         if(c.getString(iPlayListName).equals(playlistname))
            songs.add(c.getString(iSongName));
      }
      return songs;
   }
}
I have done in my app at the main app a variable of :
public static SQLiteDB myDatabase;
and i have done new to this object only once in the main activity and every time i wanted to use it i have done
the openDB function to open the DB and used the close function (both of them in the code up^^) but the problem is that
when i create the DB first time the apps start i can use it with no problem but when i reactiviate the emulator when i add more things into the DB i cant see it in the app but when i change the name of the DB , all happens again
First time can use it , when i reactivate the emulator and start the app i cant see data i wrote in the second activate of the app in the app it self
So does some body know why that happens?
Sorry for the long post :)
Thanks
 
Last edited:
Top