3

The onCreate method is not called even after deinstalling the app and then reinstalling it.

Database.java:

public class Database extends SQLiteOpenHelper {
    public static SQLiteDatabase db;
    public Database(Context context) {
        super(context,"db", null, 1);
        Log.i("DB", "dbManager");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("DB", "dbOnCreate");
        String s = "CREATE TABLE test(id INTEGER);";
        db.execSQL(s);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        Log.i("DB", "dbOnUpgrade");
    }
}

I call this:

Database db = new Database(this);

Output:

07-17 12:42:03.053 16448-16448/com.package.app I/DB: dbManager

What is this problem and how to resolve it?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • I voted to repoen this because it has nothing to do with the question proposed by @CL! – Willi Mentzel Jul 17 '17 at 08:56
  • yes, it has, read accepted answer. – Selvin Jul 17 '17 at 09:00
  • @Selvin it was marked as duplicate without even knowing if OP called getReadableDatabase or getWritetableDatabase. OP didn't show all of his code... so how can one know it? – Willi Mentzel Jul 17 '17 at 09:02
  • @selvin my problem is : onCreate method never called ! after first run and after reinstall app ! –  Jul 17 '17 at 09:03
  • 1
    @MohammadBahadori add this line getReadableDatabase() in your Database constructor and it will work. The update is only triggered if the db is accessed for writing or reading. Since you dont attempt that, onCreate is never called – Willi Mentzel Jul 17 '17 at 09:05
  • @WilliMentzel problem soled . thank you so much :) <3 –  Jul 17 '17 at 09:10
  • @MohammadBahadori If it helped you, please mark it as answer and vote up :) – Willi Mentzel Jul 17 '17 at 10:39

3 Answers3

8
public class Database extends SQLiteOpenHelper {
    public static SQLiteDatabase db;
    public Database(Context context) {
        super(context,"db", null, 1);
        getReadableDatabase(); // <-- add this, which triggers onCreate/onUpdate
        Log.i("DB", "dbManager");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i("DB", "dbOnCreate");
        // ...
    }
    // ...
}

Explanation: The update is only triggered if the db is accessed for writing or reading. Since you don't attempt that, onCreate is never called.

Usually, you would just call a method of your Database class somewhere (for example for querying entities) and that is the moment when onCreate/(or onUpdate) would be called.

Since, you don't have such methods (for now), just call it inside the Database constructor.

Or:

Alternatively, and maybe more clean would be to call getReadableDatabase() right after the creation of your Database object:

Database db = new Database(this);
db.getReadableDatabase();
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
1
@Override
public void onCreate(SQLiteDatabase db) {
   // ...
}

onCreate Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.

Try with

String s = "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY);";

Uninstall app and run again. Then check

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

You must call getReadableDatabase() or getWritableDatabase() methods for your Database class. If you want to write/update data into your database use getWritableDatabase() and if you want to get data from database use getReadableDatabase()

Database dbHelper = new Database(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
SQLiteDatabase db = dbHelper.getWritableDatabase();

Then use appropriate method for writhing, getting, updating, or deleting data in your database. for example:

db.query("table_name", null, null, null, null, null, null);
hosseinAmini
  • 2,184
  • 2
  • 20
  • 46
  • can i get getWritableDatabase in class constructor ? or must call in MainActivity ? –  Jul 17 '17 at 08:49