是否有可能在运行时创建SQLite表基础上的数组元素的数量 [英] Is it possible to Create sqlite table at run time based on number of elements in array

查看:124
本文介绍了是否有可能在运行时创建SQLite表基础上的数组元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不同的ArrayList列名。我想有一个generatized创建应根据我已通过数组列表创建表的方法。是否有可能有一个结构,可以动态地创建表。请提出任何解决方案。

 私有静态类OpenHelper扩展SQLiteOpenHelper {

        OpenHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        的System.out.println(openhelper1);

    }

    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        Log.d(** DataHelper,***********在OnCreate中);
        db.execSQL(CREATE TABLE+ TABLE_NAME +(用户名文本,altnum TEXT,口令文本,标记文本));

    }


    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        Log.w(榜样,升级数据库,这将删除表并重新创建。);
        db.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME);
        的onCreate(DB);
    }
}
 

解决方案

我创建了自己的类来创建表和generatized地插入值。

 公共无效createDynamicDatabase(上下文的背景下,字符串tableName值,ArrayList的<字符串>标题){

            Log.i(内部createLoginDatabase()方法,************* creatLoginDatabase *********);
            尝试 {

                INT I;
                字符串querryString;
                MyDatabase的= context.openOrCreateDatabase(DB,Context.MODE_WORLD_WRITEABLE,NULL); //打开数据库的写入模式。
                //System.out.println("Table名称:+ tableName.get(0));

                querryString = title.get(0)+VARCHAR(30),;
                Log.d(** createDynamicDatabase,中的OnCreate);
                对于(i = 1; I< title.size() -  1;我++)
                {
                    querryString + = title.get(ⅰ);
                    querryString + =VARCHAR(30);
                    querryString + =;
                }
                querryString + = title.get(ⅰ)+VARCHAR(30);

                querryString =CREATE TABLE IF NOT EXISTS+ tableName值+(+ querryString +);

                的System.out.println(CREATE TABLE语句:+ querryString);

                myDataBase.execSQL(querryString);

            }赶上(前的SQLException){
                Log.i(CREATEDB异常,ex.getMessage());
            }
        }
        公共无效插入(上下文的背景下,ArrayList的<字符串> array_vals,ArrayList的<字符串>标题字符串TABLE_NAME){
            Log.d(内部插入,插入的开始对表的名称:+ TABLE_NAME);
            MyDatabase的= context.openOrCreateDatabase(DB,Context.MODE_WORLD_WRITEABLE,NULL); //打开数据库的写入模式。
            字符串titleString = NULL;
            字符串markString = NULL;
            INT I;
            titleString = title.get(0)+,;
            ?,markString =;
            Log.d(** createDynamicDatabase,中的OnCreate);
            对于(i = 1; I< title.size() -  1;我++)
            {
                titleString + = title.get(ⅰ);
                titleString + =;
                ?,markString + =;
            }
            titleString + = title.get(ⅰ);
            markString + =?;

            //System.out.println("Title字符串:+ titleString);
            //System.out.println("Mark字符串:+ markString);


            INSERT =插入+ TABLE_NAME +(+ titleString +)+值+(+ markString +);
            的System.out.println(Insert语句:+ INSERT);
            //System.out.println("Array大小IIIIII :::+ array_vals.size());
            //this.insertStmt = this.myDataBase.compileStatement(INSERT);
            INT S = 0;

            而(S< array_vals.size()){

            的System.out.println(ARRAY1的大小+ array_vals.size());
                    阵列//System.out.println("Size+ title.size());
            INT J = 1;
            this.insertStmt = this.myDataBase.compileStatement(INSERT);
            对于(INT K = 0; K< title.size(); k ++)
            {

                列//System.out.println("Value+标题+就是+ 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 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);
    }
}

解决方案

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

这篇关于是否有可能在运行时创建SQLite表基础上的数组元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆