Can't create SQLite database in my android app -
i'm using application object(android building bloc) call database constructor inside oncreate method. can't figure out why can't created. how code looks like:
my database class, dbhelper 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); } { db.close(); } } /** * deletes data */ public void delete() { // open database sqlitedatabase db = dbhelper.getwritabledatabase(); // delete data db.delete(table, null, null); // close database db.close(); } }
and here application class call constructor create 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 in 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
the database won't created until ask access it. documentation:
create helper object create, open, and/or manage database. method returns quickly. database not created or opened until 1 of getwritabledatabase() or getreadabledatabase() called.
Comments
Post a Comment