java - Android - How to Create Sqlite database with Tables -
this question has answer here:
i need create database 3 tables , doing below:
public class databaseutils extends sqliteopenhelper { private final context mycontext; private sqlitedatabase database; // database creation sql statement private static final string create_table_cs = "create table "+ table_cs + "(" + column_cs + " text not null, " + column_ce_cid + " integer not null, "+ column_ce_pid +" integer not null);"; private static final string create_table_ss = "create table "+ table_ss + "(" + column_ss + " text not null, " + column_sub_cid + " integer not null, "+ column_sub_pid +" integer not null);"; private static final string create_table_as = "create table "+ table_as + "(" + column_as + " text not null, " + column_cid + " integer not null, "+ column_aid +" integer not null);"; public databaseutils(context context) { super(context, database_name, null, database_version); database_path = environment.getdatadirectory().getabsolutepath() + "/" +"data/"+ context.getresources().getstring(r.string.app_package); this.mycontext = context; } @override public void oncreate(sqlitedatabase database) { database.execsql(create_table_cs); database.execsql(create_table_ss); database.execsql(create_table_as); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(databaseutils.class.getname(),"upgrading database version " + oldversion + " "+ newversion + ", destroy old data"); //db.execsql("drop table if exists " + table_comments); oncreate(db); } }
and in activity calling databaseutils
class in oncreate
below:
databaseutils db = new databaseutils(this);
but database not creating 3 tables. doing wrong? btw, have string values correctly. please me how create database.
i found solution. databaseutils
' oncreate()
never called if implement below:
databaseutils db = new databaseutils(this);
in myactivity
's oncreate()
method. need call getwritabledatabase()
in myactivity below:
databaseutils db = new databaseutils(this); db.getwritabledatabase();
then databaseutils
' oncreate()
called , tables created.
Comments
Post a Comment