Is it possible to Create sqlite table at run time based on number of elements in array

I have a different arraylist with column names. I want to have a generatized create method that should create table based on the arraylist i have passed. Is it possible to have a structure with can create table dynamically. Please suggest any solution.

private static class OpenHelper extends SQLiteOpenHelper {

        OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        System.out.println("openhelper1");

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("**DataHelper", "***********in oncreate");
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (username TEXT,altnum TEXT,passkey TEXT,flag TEXT)");

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("Example", "Upgrading database, this will drop tables and recreate.");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

  • Android Studio/Gradle build can't find custom Application class
  • Google Places API for Android - PlacePikcer without search option
  • Android dual SIM card API
  • read failed: EBADF (Bad file number)
  • Use Android Annotations in custom dialog class
  • TimerTask vs Thread.sleep vs Handler postDelayed - most accurate to call function every N milliseconds?
  • Related posts:

    How to center drawableTop and text vertically on the Button in Android?
    Is there a faster video rendering solution than using AndroidBitmap_xxx functions?
    Android: How to get current device WiFi-direct name
    How to get ndk-gdb working on Android?
    where does ansroid.os.SystemProperties store its key/values?
    Rotate a saved bitmap in android
  • How to bind to a port less than 1024 in Android?
  • android:enable and services
  • How to preserve line breaks in xml <string> resources in Android?
  • Android XML Layout for all Devices (Small/Normal/Large/XLarge, etc)
  • How do I integrate Paytm wallet in an Android application?
  • Transparent WebView not working on Android v4.0
  • One Solution collect form web for “Is it possible to Create sqlite table at run time based on number of elements in array”

    I created my own class to create tables and insert values in generatized manner.

    public void createDynamicDatabase(Context context,String tableName,ArrayList<String> title) {
    
                Log.i("INSIDE createLoginDatabase() Method","*************creatLoginDatabase*********");
                try {
    
                    int i;
                    String querryString;
                    myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_WRITEABLE, null);         //Opens database in writable mode.
                    //System.out.println("Table Name : "+tableName.get(0));
    
                    querryString = title.get(0)+" VARCHAR(30),";
                    Log.d("**createDynamicDatabase", "in oncreate");
                    for(i=1;i<title.size()-1;i++)
                    {               
                        querryString += title.get(i);
                        querryString +=" VARCHAR(30)";
                        querryString +=",";
                    }
                    querryString+= title.get(i) +" VARCHAR(30)";
    
                    querryString = "CREATE TABLE IF NOT EXISTS " + tableName + "("+querryString+");";
    
                    System.out.println("Create Table Stmt : "+ querryString);
    
                    myDataBase.execSQL(querryString);
    
                } catch (SQLException ex) {
                    Log.i("CreateDB Exception ",ex.getMessage());
                }
            }
            public void insert(Context context,ArrayList<String> array_vals,ArrayList<String> title,String TABLE_NAME) {
                Log.d("Inside Insert","Insertion starts for table name: "+TABLE_NAME);
                myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_WRITEABLE, null);         //Opens database in writable mode.
                String titleString=null;
                String markString= null;
                int i;
                titleString = title.get(0)+",";
                markString = "?,";
                Log.d("**createDynamicDatabase", "in oncreate");
                for(i=1;i<title.size()-1;i++)
                {               
                    titleString += title.get(i);
                    titleString +=",";
                    markString += "?,";
                }
                titleString+= title.get(i);
                markString += "?";
    
                //System.out.println("Title String: "+titleString);
                //System.out.println("Mark String: "+markString);
    
    
                INSERT="insert into "+ TABLE_NAME + "("+titleString+")"+ "values" +"("+markString+")";
                System.out.println("Insert statement: "+INSERT);
                //System.out.println("Array size iiiiii::: "+array_vals.size());
                //this.insertStmt = this.myDataBase.compileStatement(INSERT);
                int s=0;
    
                while(s<array_vals.size()){
    
                System.out.println("Size of array1"+array_vals.size());
                        //System.out.println("Size of array"+title.size());
                int j=1;
                this.insertStmt = this.myDataBase.compileStatement(INSERT);
                for(int k =0;k< title.size();k++)
                {
    
                    //System.out.println("Value of column "+title+" is "+array_vals.get(k+s));
                    //System.out.println("PRINT S:"+array_vals.get(k+s));
                    System.out.println("BindString: insertStmt.bindString("+j+","+ array_vals.get(k+s)+")");
                    insertStmt.bindString(j, array_vals.get(k+s));
    
    
    
                    j++;
                }
    
                s+=title.size();
    
                }
                insertStmt.executeInsert();
            }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.