I am working on sqlite and android app. There is a xml file, i take the information of database, tables, tables count, column types... When app is starts, all tables should be created. There is no specific count of tables and the table columns. So I should design a percect dynamic structure. Actually I did too thing; Dynamic sqlquery for unknown tables count... Left some 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 and types...
private String querybuild(String [] ss){
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i<ss.length;i++){
if( i % 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 is onCreate method. May be i should call second time, i dont know...
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(querybuild(tables));
}
So there is point. Where app is created, the codes is 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 is working, that is creates table name of tables1. But the second variable dbc does not creates table. So my design is not good...
I know the database version for onupgrade. But already app is starting new and i want created all tables in first time. Should i call the onCreate method second time? Or should i change the database version and use onupgrade? What is the way?
Aucun commentaire:
Enregistrer un commentaire