0

I creared database and runs it. Then I added couple rows. Everything runs as it should. Then I wanted to keep track when the last date was done into the table. So I add one more table into the same database name and try to save current date but when I run it the table could not be found. What I got so far: in MYSQLiteHelper class

//impotrs...
public class MYSQLiteHelper extends SQLiteOpenHelper {


 // TABLE EMPLOYEES
    public static final String TABLE_NAME = "employee";
        public static final String COLUMN_ID = "_id";
        public static final String COLUMN_NAME = "name";
        public static final String COLUMN_EMAIL = "email";
        public static final String COLUMN_TIME = "saved_date";

 //TABLE DATE
        public static final String COLUMN_TIME_ID = "date_id";
        public static final String TABLE_TIME_NAME = "date"; 

  //DATABASE COMMENTS.DB
        private static final String DATABASE_NAME = "commments.db";
        private static final int DATABASE_VERSION = 1;


        // Database creation sql statement for employee
        private static final String DATABASE_CREATE = "create table "
                + TABLE_NAME + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_NAME + " text not null, " + COLUMN_EMAIL + " text)";

  // Database creation sql statement for date
        private static final String DATABASE_CREATE_TIME = "create table " 
                + TABLE_TIME_NAME + "(" + COLUMN_TIME_ID + " integer primary key autoincrement, " + COLUMN_TIME + " text)";

        public MYSQLiteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }


        @Override
        public void onCreate(SQLiteDatabase database) {
            database.execSQL(DATABASE_CREATE_TIME); 
            database.execSQL(DATABASE_CREATE);

        }


        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(MYSQLiteHelper.class.getName(),"Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_TIME_NAME);
            Log.w(MYSQLiteHelper.class.getName(),"Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }

The table called employee is running perfecly. I can add new employee or select any queries. But when I try to insert current date into the table name date it has run time error "android.database.qslte.SQLiteException: no such table(code 1);, while compiling SETECT date_id, seved_date From date. What I have in other classes. Mostly I did the same what I have done for employye: in main Activity button:

  EmployeeDataSource dataSource = new EmployeeDataSource(this);

    // other codes....

    public void getCurrentDate(View v){

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formatDate = sdf.format(new Date());
            txtTop.setText(formatDate);
            dataSource.addDate(formatDate);

        }

in EmployeeDataSource class:

 private String[] timeColumn = {MYSQLiteHelper.COLUMN_TIME_ID ,MYSQLiteHelper.COLUMN_TIME};


      public long addDate(String date){
          ContentValues values = new ContentValues();
          values.put(MYSQLiteHelper.COLUMN_TIME, date);
          return database.insert(MYSQLiteHelper.TABLE_TIME_NAME, null, values);
      }  

public String getStoredDate(){
          ArrayList<String> array = new ArrayList<String>();
          Cursor crs = database.query(MYSQLiteHelper.TABLE_TIME_NAME, timeColumn, null, null, null, null, null);
         crs.moveToFirst();
         while (!crs.isAfterLast()){
             array.add(crs.getString(1));
             crs.moveToNext();
         }
          String strData = array.get(array.size() - 1);
          return strData;
      }

It seems to me that I ommit or have something wrong in MYSQLiteHelper as I read that onCreate() method is called only when the database file does not exist but my database file already exist. Well can somebody please help. Thank you.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
David Sajdl
  • 164
  • 1
  • 10
  • 1
    no such table error. Check if query is proper. And if you added this table after running app in device, try uninstalling and installing again – MysticMagicϡ Dec 20 '14 at 17:07

1 Answers1

1

You need to increment the DATABASE_VERSION constant so that onUpgrade will be called and the table will be created.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • Thanks it works. The previous data was lost but now both tables can store and update data. – David Sajdl Dec 20 '14 at 17:55
  • If you need to maintain the user data you'll need to add code to `onUpgrade` that will compare what version you have and what version you are moving to, and only make the schema changes required. Instead of dropping both tables and calling onCreate. – Greg Ennis Dec 20 '14 at 18:00