Home > front end >  How to create two tables in one db file?
How to create two tables in one db file?

Time:01-08

I want to create two tables in one db file, but it is not working as expected.

public class DBHelper extends SQLiteOpenHelper {
    public static final String DBNAME = "Bocchi.db";
    public DBHelper(Context context) {
        super(context, "Bocchi.db", null, 1);
    }

    public static final String TABLE_NAME1 = "users";
    public static final String TABLE_NAME2 = "breakfast";
    @Override
    public void onCreate(SQLiteDatabase MyDB) {
        String table1 = "CREATE TABLE " TABLE_NAME1 "(username TEXT PRIMARY KEY, password TEXT)";
        String table2 = "CREATE TABLE " TABLE_NAME2 "(username TEXT PRIMARY KEY, energy INT)";
        MyDB.execSQL(table1);
        MyDB.execSQL(table2);
    }

Why am I doing like on video but it cannot create two tables. I have checked the db file, but it only has one table.

CodePudding user response:

You can try the onUpgrade method like below

@Override
public void onUpgrade(SQLiteDatabase _db, int oldVer, int newVer){
    onCreate(_db);
}
  • Related