I am working on sqlite-android app. Actually problem is, that; There is not specific table name, table count, column count... It is dynamic.So there is must be dynamic create table query. When app is starts, the tables should be created. I do that with sqliteopenhelper. My activity is call my Database activity. Database activity have onCreate method. The method is works my create table query.
The all code is works like good. But it is creates just table1. Not table2 or more. How can i create tables like that. Actually that is the point; The xml gives me information about of database. You know, there is must be perfect dynamic structure. Any idea about that? I know little use database version. It is working. But my app is already new starts, not necessary with db version. Or should i use call the onCreate method?
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 there are how many tables
public 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+")";
}
The onCreate:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(querybuild(tables));
}
My other activity
SQLiteDatabase db = new Database(getApplicationContext(),"table1",new String[]{ "ID","INTEGER PRIMARY KEY AUTOINCREMENT"
,"ADD","TEXT NOT NULL","BDD","TEXT","CDD","TEXT"}).getWritableDatabase();
SQLiteDatabase dsb = new Database(getApplicationContext(),"table2",new String[]{ "ID","INTEGER PRIMARY KEY AUTOINCREMENT"
,"SSS","TEXT","TTT","TEXT"}).getWritableDatabase();
Aucun commentaire:
Enregistrer un commentaire