0

I want to add another table in my existing Sqlite database for my android application. I have created one table "dailytips" and want to add another table "healthytips". i have tried different ways but didn't succeed and also done google search but every code is different from mine. Please some one help me in this.

Here is my code:

 package com.example.health;

 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.database.sqlite.SQLiteDatabase.CursorFactory;

 public class SQLiteAdapter {

 public static final int DATABASE_VERSION = 2;
 public static final String DATABASE_NAME = "healthDB";

 public static final String TABLE_DAILY = "dailytips";
 public static final String COLUMN_ID = "_id";
 public static final String COLUMN_TIPS = "tips";

 public static final String TABLE_HEALTH="healthytips";
 public static final String COLUMN_H_ID = "_id";
 public static final String COLUMN_H_TIPS = "tips";

 //create table TABLE_DAILY (ID integer primary key, tips text not null);
 private static final String CREATE_TABLE_DAILYS =
 "create table " + TABLE_DAILY + " ("
 + COLUMN_ID + " integer primary key autoincrement, "
 + COLUMN_TIPS + " text not null);";

 //create table TABLE_HEALTHY (ID integer primary key, tips text not null);
 private static final String CREATE_TABLE_HEALTH=
 "create table " + TABLE_HEALTH + " ("
 + COLUMN_H_ID + " integer primary key autoincrement, "
 + COLUMN_H_TIPS + " text not null);";

 private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;

 private Context context;

 public SQLiteAdapter(Context c){
 context = c;
  }

 public SQLiteAdapter openToRead() throws android.database.SQLException {
 sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
 }

 public SQLiteAdapter openToWrite() throws android.database.SQLException {
 sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
 sqLiteDatabase = sqLiteHelper.getWritableDatabase();
 return this;
 }

 public void close(){
 sqLiteHelper.close();
 }

 public long insert(String tips){

 ContentValues contentValues = new ContentValues();
 contentValues.put(COLUMN_TIPS, tips);
 return sqLiteDatabase.insert(TABLE_DAILY, null, contentValues);
 }

 public int deleteAll(){
 return sqLiteDatabase.delete(TABLE_DAILY, null, null);
 }

 public Cursor queueAll(){
 String[] columns = new String[]{COLUMN_ID, COLUMN_TIPS};
 Cursor cursor = sqLiteDatabase.query(TABLE_DAILY, columns,
   null, null, null, null, null);
 return cursor;
 }

 public class SQLiteHelper extends SQLiteOpenHelper {

 public SQLiteHelper(Context context, String name,
 CursorFactory factory, int version) {
 super(context, name, factory, version);
  }

 @Override
 public void onCreate(SQLiteDatabase db) {
 // TODO Auto-generated method stub
 db.execSQL(CREATE_TABLE_DAILYS);
 db.execSQL(CREATE_TABLE_HEALTH);
  }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // TODO Auto-generated method stub

 }
 }}
Zohaib
  • 41
  • 12
  • Where you are running the code eclipse or android studio? – sherin Jun 26 '14 at 07:15
  • Uninstall your app so the old database file is removed. Then install and run again so the database helper `onCreate()` is run. http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto Jun 26 '14 at 07:23

2 Answers2

0

In the onCreate() method,

    db.execSQL(CREATE_DATABASE_TABLE_1);
    db.execSQL(CREATE_DATABASE_TABLE_2);

Update this in you program.

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // TODO Auto-generated method stub
      // Drop older table if existed

        db.execSQL("DROP TABLE IF EXISTS DATABASE_TABLE_1");
        db.execSQL("DROP TABLE IF EXISTS DATABASE_TABLE_2");

      // Create tables again
      onCreate(db);
     }

Note: need to clear database before you run the application.

sherin
  • 1,091
  • 2
  • 17
  • 27
0

My solution to this is create a Database helper derived from SqliteDatabaseHelper and in onCreate method add all your SQL statements into it including new and old as well as in onUpgrade method, so next time whether you create a new database ( may be on first run) or upgrade your database you will end up with all new old tables. Use this class only to create or upgrade db , rest of the work you will do in other more specific classes ex.

DatabaseHelper -- add code to create /upgrade all your tables Table1Helper - methods to manage table1 Table2Helper - methods to manage table2 etc.

Hope this helps someone.

vishal dharankar
  • 7,536
  • 7
  • 57
  • 93