android - How to build more effective design for sqlite operations -
i working on sqlite , android app. there xml file, take information of database, tables, tables count, column types... when app starts, tables should created. there no specific count of tables , table columns. should design percect dynamic structure. did thing; dynamic sqlquery unknown tables count... left easy thing.
my constructor ;
public database(context context,string tablename, string [] a) { super(context, database_name, null, database_version); this.tables=a; this.table_name=tablename; }
my query builder, not matter how many tables count , types...
private string querybuild(string [] ss){ stringbuilder stringbuilder = new stringbuilder(); for(int i=0;i<ss.length;i++){ if( % 2 == 0){ stringbuilder.append(ss[i]); stringbuilder.append(" "); } if(i % 2 == 1){ stringbuilder.append(ss[i]); if(i!=ss.length-1) stringbuilder.append(","); } } string finalstring = stringbuilder.tostring(); return "create table if not exists " + table_name + "(" +finalstring+")"; }
it oncreate method. may should call second time, dont know...
@override public void oncreate(sqlitedatabase db) { db.execsql(querybuild(tables)); }
so there point. app created, codes here;
sqlitedatabase dbp= new database(getapplicationcontext(),"table1",new string[]{ "id","integer primary key autoincrement","cola","text not null","colb","text"}).getwritabledatabase(); sqlitedatabase dbc= new database(getapplicationcontext(),"table2",new string[]{ "_id","integer primary key autoincrement","col1","text not null","col2","text","col3","text"}).getwritabledatabase();
the first dbp variable working, creates table name of tables1. second variable dbc not creates table. design not good...
i know database version onupgrade. app starting new , want created tables in first time. should call oncreate method second time? or should change database version , use onupgrade? way?
sqliteopenhelper
intended used database structure known.
anyway, problem you're using oncreate
wrong; responsibility create entire database. have handle tables in there:
@override public void oncreate(sqlitedatabase db) { // ... list of tables ... (each table) { db.execsql(querybuild(table)); } }
if cannot write oncreate
way, cannot use sqliteopenhelper
, must use plain sqlitedatabase
object directly.
Comments
Post a Comment