简体   繁体   English

无法在android SQLite DB中插入多行

[英]cannot able to insert more than one row in android SQLite DB

I am working on a project where 6 edittext fields are inserted into the the database when i click the save button and it will be viewed in a list... I can able to achieve those things but problem is that i could not able to insert multiple rows into the DB. 我正在开发一个项目,当我单击保存按钮时,将6个edittext字段插入到数据库中,它将在列表中查看...我能够实现这些目的但问题是我无法插入多行进入DB。 The listview is showing only the first inserted values always eventhough if i insert again. 如果我再次插入,listview只显示第一个插入的值总是eventhough。 Please help me for this solution.... 请帮我解决这个问题....
My code for table creation and insertion is as follows for your reference 表格创建和插入的代码如下所示供您参考

public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String CREATE_TABLE_QUERY = null;
            // Used
            CREATE_TABLE_QUERY = "CREATE TABLE IF NOT EXISTS userMaster(Bar CHAR(25),Product CHAR(15), Name TEXT,Mrp CHAR(15),Quantity CHAR(15), Tax CHAR(15));";
            db.execSQL(CREATE_TABLE_QUERY);
            L.i("userMaster Table created");

        }

        public void insertDetails(String bar, String mrp, String quantity,
                String tax, String product, String name) {
            SQLiteDatabase dbase = this.getReadableDatabase();
            ContentValues cv = new ContentValues();
            cv.put("bar", bar);
            cv.put("product", product);
            cv.put("name", name);

            cv.put("mrp", mrp);
            cv.put("quantity", quantity);
            cv.put("tax", tax);

            dbase.insert("userMaster", null, cv);
            dbase.close();
        } <br> 
public void insertUserMaster(String bar, String mrp, String quantity,
            String tax, String product, String name) {

        SQLiteDatabase SqlDB = getWritableDatabase();

        SqlDB.delete("userMaster", null, null);

        String INSERT = "insert into userMaster (Bar,Product,Name,Mrp,Quantity,Tax) values (?,?,?,?,?,?)";
        SQLiteStatement insertstatment = SqlDB.compileStatement(INSERT);
        insertstatment.bindString(1, bar);
        insertstatment.bindString(2, product);
        insertstatment.bindString(3, name);
        insertstatment.bindString(4, quantity);
        insertstatment.bindString(5, mrp);
        insertstatment.bindString(6, tax);
        insertstatment.executeInsert();
        SqlDB.close();
        close();

    } <br>

The array list representation is as follows 数组列表表示如下

public ArrayList<HashMap<String, String>> getdata() {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query("userMaster", null, null, null, null, null,
                null, null);
        ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
        if (cursor != null) {
            if (cursor.getCount() > 0 && cursor.moveToFirst()) {
                do {
                    HashMap<String, String> hm = new HashMap<String, String>();

                    hm.put("bar", cursor.getString(0));
                    hm.put("product", cursor.getString(1));
                    hm.put("name", cursor.getString(2));
                    hm.put("mrp", cursor.getString(3));
                    hm.put("quantity", cursor.getString(4));
                    hm.put("tax", cursor.getString(5));
                    arraylist.add(hm);
                } while (cursor.moveToNext());
            }

        }

        cursor.close();
        db.close();

        return arraylist;

    }

In method insertUserMaster you delete all data before insert. 在方法insertUserMaster您在插入之前删除所有数据。

Remove this line: 删除此行:

SqlDB.delete("userMaster", null, null);

Use proper query to avoid those issue. 使用正确的查询来避免这些问题。 Before inserting check whether you are deleting the existing table.. 在插入之前检查是否要删除现有表..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM