从动态创建的列中获取所有值

时间:2018-08-12 12:51:42

标签: java android sqlite arraylist

我创建了一些具有可变列的表,很少有4列,很少有10列,这取决于我得到的json响应。为了使表具有可变列并将数据插入其中,我在下面的代码中使用了

//******************for creating below code*****************
    public void createDynamicDatabase(Context context,String tableName,ArrayList<String> title) {

                Log.i("INSIDE createLoginDatabase() Method","*************creatLoginDatabase*********");
                try {

                    int i;
                    String queryString;
                    myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_WRITEABLE, null);         //Opens database in writable mode.
                    //System.out.println("Table Name : "+tableName.get(0));

                    queryString = title.get(0)+" VARCHAR(30),";
                    Log.d("**createDynamicDatabase", "in oncreate");
                    for(i=1;i<title.size()-1;i++)
                    {               
                        queryString += title.get(i);
                        queryString +=" VARCHAR(30)";
                        queryString +=",";
                    }
                    queryString+= title.get(i) +" VARCHAR(30)";

                    queryString = "CREATE TABLE IF NOT EXISTS " + tableName + "("+queryString+");";

                    System.out.println("Create Table Stmt : "+ queryString);

                    myDataBase.execSQL(queryString);

                } catch (SQLException ex) {
                    Log.i("CreateDB Exception ",ex.getMessage());
                }
            }

    //******************for inserting below code*****************


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

我无法使用以下代码从表中获取值,我在不同的表中具有不同的列,因此如何使用以下代码从该表中获取值,我只能获取固定的列,例如“ cursor.getInt(0) ,cursor.getString(1)”等,但我希望动态地使用它,我希望动态地传递表名并从该表中获取所有值

public ArrayList<Product> getListProduct(String TABLENAME) {
        Product product = null;
        ArrayList<Product> productList = new ArrayList<>();
        openDatabase();
        Cursor cursor = mDatabase.rawQuery("SELECT * FROM "+TABLENAME, null);
        cursor.moveToFirst();
        int countit=cursor.getColumnCount();
        System.out.println("countit"+countit);
        while (!cursor.isAfterLast()) {


     System.out.println("cursor=="+cursor.isAfterLast());
//************SOMETHING HAS TO BE DONE HERE TO FETCH FROM DYNAMIC/VARIABLE COLUMNS BECAUSE COLUMN NUMBER VARY FROM TIME TO TIME NOT SURE WHAT TO DO***********
           // for(int i = 0; i < cursor.getColumnCount(); i ++){
                product = new Product(cursor.getInt(0), cursor.getString(1), cursor.getInt(2));
                productList.add(product);
                cursor.moveToNext();
            // }



        }
        cursor.close();
        closeDatabase();
        return productList;
    }

模型类,将用于从sqlite数据库填充进一步的listview数据

public class Product {
    private int id;
    private String name;
    private int price;
    private String description;

    public Product(int id, String name, int price) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

0 个答案:

没有答案