Unable to create SQLite database in Android application

I use my application object (Android block for development) to call the database constructor inside my oncreate method. But I can’t understand why it cannot be created. This is what my code looks like:

my database class, with DbHelper as an inner class:

package com.example.pharmacie; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class PhData { private static final String TAG = PhData.class.getSimpleName(); private static final int VERSION = 1; private static final String DATABASE = "ph_info.db"; private static final String TABLE = "ph_info"; public static final String C_ID = "_id"; public static final String C_CREATED_AT = "created_at"; public static final String C_NAME = "Pharmacie "; public static final String C_TELE = " "; public static final String C_ADRESS = "Tet"; public static final String C_HAS_SHIFT = "yes"; // yes if night shift public static final String C_SCHEDULE = "YYYY-MM-DD"; private static final String GET_ALL_ORDER_BY = C_CREATED_AT + " DESC"; private static final String[] MAX_CREATED_AT_COLUMNS = { "max(" + PhData.C_CREATED_AT + ")" }; Context context; private DbHelper dbHelper; // Inner class: DbHelper implementations class DbHelper extends SQLiteOpenHelper { public DbHelper() { super(context, DATABASE, null, VERSION); Log.d(TAG, "Constructor called"); } @Override public void onCreate(SQLiteDatabase db) { Log.d(TAG, "Creating database: " + DATABASE); db.execSQL("create table " + TABLE + " (" + C_ID + " int primary key, " + C_CREATED_AT + " int, " + C_NAME + " text, " + C_ADRESS + " text, " + C_HAS_SHIFT + " text, " + C_SCHEDULE + " text, " + C_TELE + " text)"); Log.d(TAG, "database created"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table " + TABLE); this.onCreate(db); } } // end of dbhelper class public PhData(Context context) { this.context = context; this.dbHelper = new DbHelper(); Log.d(TAG, "Initialized data"); } public void close() { this.dbHelper.close(); } public void insertOrIgnore(ContentValues values) { Log.d(TAG, "insertOrIgnore on " + values); SQLiteDatabase db = this.dbHelper.getWritableDatabase(); try { db.insertWithOnConflict(TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE); } finally { db.close(); } } /** * Deletes ALL the data */ public void delete() { // Open Database SQLiteDatabase db = dbHelper.getWritableDatabase(); // Delete the data db.delete(TABLE, null, null); // Close Database db.close(); } } 

And here is my application class, where I call the constructor to create my db:

 package com.example.pharmacie; import android.app.Application; import android.util.Log; public class PharmacieApplication extends Application { private static final String TAG = PharmacieApplication.class.getSimpleName(); PhData phData; @Override public void onCreate() { super.onCreate(); phData = new PhData(this); Log.d(TAG, "onCreated"); } @Override public void onTerminate() { super.onTerminate(); Log.d(TAG, "onTerminated"); } } 

And here is what I get in my logcat:

 04-20 03:15:05.968: D/PhData(368): Constructor called 04-20 03:15:05.968: D/PhData(368): Initialized data 04-20 03:15:05.968: D/PharmacieApplication(368): onCreated 04-20 03:15:06.568: D/JoursListActivity(368): onCreated 
+4
source share
3 answers

The database will not actually be created until you ask for it. From the doc:

Create a helper object to create, open, and / or manage the database. This method always returns very quickly. The database is actually created or opened before one of getWritableDatabase () or getReadableDatabase () is called.

+3
source

I am changing your code by putting this code and trying.

 public class PhData { private static final String TAG = PhData.class.getSimpleName(); private static final int VERSION = 1; private static final String DATABASE = "ph_info.db"; private static final String TABLE = "ph_info"; public static final String C_ID = "_id"; public static final String C_CREATED_AT = "created_at"; public static final String C_NAME = "Pharmacie "; public static final String C_TELE = " "; public static final String C_ADRESS = "Tet"; public static final String C_HAS_SHIFT = "yes"; // yes if night shift public static final String C_SCHEDULE = "YYYY-MM-DD"; private static final String GET_ALL_ORDER_BY = C_CREATED_AT + " DESC"; private static final String[] MAX_CREATED_AT_COLUMNS = { "max(" + PhData.C_CREATED_AT + ")" }; Context context; private DbHelper dbHelper; private SQLiteDatabase db; // Inner class: DbHelper implementations class DbHelper extends SQLiteOpenHelper { public DbHelper() { super(context, DATABASE, null, VERSION); Log.d(TAG, "Constructor called"); } @Override public void onCreate(SQLiteDatabase db) { Log.d(TAG, "Creating database: " + DATABASE); db.execSQL("create table " + TABLE + " (" + C_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " + C_CREATED_AT + " TEXT, " + C_NAME + " text, " + C_ADRESS + " text, " + C_HAS_SHIFT + " text, " + C_SCHEDULE + " text, " + C_TELE + " text)"); Log.d(TAG, "database created"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table " + TABLE); onCreate(db); } } // end of dbhelper class public PhData(Context context) { this.context = context; this.dbHelper = new DbHelper(context); db=this.dbHelper.getWritableDatabase(); db=this.dbHelper.getReadableDatabase(); Log.d(TAG, "Initialized data"); } public void close() { if (db != null) db.close(); } public Database open() throws SQLException{ this.dbHelper= new ContactsDBHelper(con); db=this.dbHelper.getWritableDatabase(); db=this.dbHelper.getReadableDatabase(); return this; } public void insertOrIgnore(ContentValues values) { Log.d(TAG, "insertOrIgnore on " + values); SQLiteDatabase db = this.dbHelper.getWritableDatabase(); try { db.insertWithOnConflict(TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE); } finally { db.close(); } } /** * Deletes ALL the data */ public void delete() { // Open Database SQLiteDatabase db = dbHelper.getWritableDatabase(); // Delete the data db.delete(TABLE, null, null); // Close Database db.close(); } } 

May be useful for you.

0
source

Actually, I discovered the original problem that led me to another problem: I need to pass the dbHelper context to the constructor in order to call it from my Application class.

Another created by erro:

 public static final String C_SCHEDULE = "YYYY-MM-DD"; 

you cannot use the minus sign as a separator in creating db because the virtual machine treats it as a mathematical sign. therefore, my declaration should have been changed to:

 public static final String C_SCHEDULE = "YYYY_MM_DD"; 
0
source

All Articles