I am working an android app with sqlite. There is xml file on web. The xml gives me information of database.(databasename, tables name, tables columns...) So there is must be dynamic create table query.
I do that. For unknown tables column count. That returns a good sql query string...
String table1[] =new String[]{ "_ID","INTEGER PRIMARY KEY AUTOINCREMENT","col1","TEXT NOT NULL","col1","TEXT"};
public String querybuild(String [] tablearray){
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i<tablearray.length;i++){
if( i % 2 == 0){
stringBuilder.append(tablearray[i]);
stringBuilder.append(" ");
}
if(i % 2 == 1){
stringBuilder.append(tablearray[i]);
if(i!=tablearray.length-1)
stringBuilder.append(",");
}
}
String finalString = stringBuilder.toString();
return "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("+finalString+")";
}
But there is a problem. My onCreate method in extended SqliteOpenHelper class is can working just one time. like this;
db.execSQL(querybuild(table1));
I think, i should create more String array for the tables array. like that;
String[][] alltables = { table1, table2 };
And the onCreate must have a loop, it will like this;
for ()
{
db.execSQL(querybuild(alltables)); // it must exec all table in the array!
}
Yes that is the point. Any idea? How it would be, the loop. Please Help me. I am so tired for that. I know Database Version, but its not good way.
Aucun commentaire:
Enregistrer un commentaire