是否可以在运行时根据数组中的元素数量创建 sqlite 表

发布于 2024-12-04 13:19:21 字数 820 浏览 0 评论 0原文

我有一个带有列名的不同数组列表。我想要一个生成的创建方法,该方法应该根据我传递的数组列表创建表。是否有可能有一个可以动态创建表的结构。请提出任何解决方案。

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);
    }
}

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2024-12-11 13:19:21

我创建了自己的类来创建表并以生成的方式插入值。

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();
        }

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();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文