1

Here is my code of my database. How could I add a new table to it?

public class DatabaseHelper extends SQLiteOpenHelper {

    // Table Name
    public static final String TABLE_NAME = "COUNTRIES";


    // Table columns
    public static final String _ID = "_id";
    public static final String SUBJECT = "subject";
    public static final String DESC = "description";
    public static final String DESC2 = "description2";
    public static final String DESC3 = "description3";

    // Database Information
    static final String DB_NAME = "Ar.DB";

    // database version
    static final int DB_VERSION = 3;

    // Creating table query
    private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SUBJECT + " TEXT NOT NULL, " + DESC + " TEXT, " + DESC2 + " TEXT, " + DESC3 + " TEXT);";

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

If anyone knows just add the code for the insertion of a new table, so that this could also make it easy for me.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Arun Ganessh
  • 81
  • 1
  • 4
  • 2
    Possible duplicate of [How to create a two table in single Database in Android Application?](https://stackoverflow.com/questions/8604441/how-to-create-a-two-table-in-single-database-in-android-application) – Learning Always Jul 12 '18 at 09:39
  • https://stackoverflow.com/questions/22504079/how-to-add-a-new-table-in-existing-database – Adil Jul 12 '18 at 10:40

2 Answers2

0

In onUpgrade() method you can add new table in existing database, like this:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  if (newVersion == 4) {
     // Create New Table
  }
}

Note: You have to change DB_VERSION = 4 for call onUpgrade

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
0

Create another string query for the second table which you want to insert in the database.

Something like this.

public class DBHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "my_db";

private static final int DATABASE_VERSION = 1;

// Database creation sql statement
public static final String Table1 = "create table table1name ("Your columns");";


public static final String Table2 = "create table table2name ("Your columns");";


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

// Method is called during creation of the database
@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(Table1);
   database.execSQL(Table2);

}

// Method is called during an upgrade of the database, e.g. if you increase
// the database version
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
        int newVersion) {

    database.execSQL("DROP TABLE IF EXISTS " + Table1);
    database.execSQL("DROP TABLE IF EXISTS " + Table2);

    onCreate(database);
}


public boolean deleteDatabase(Context context) {
        return context.deleteDatabase(DATABASE_NAME);
}

}

Now create the CRUD methods for each of the table afterwards.

Sarthak Grover
  • 248
  • 4
  • 15