我正在开发一个项目,当我单击保存按钮时将6个edittext字段插入到数据库中,它将在列表中查看...我能够实现这些目标,但问题是我不能能够在DB中插入多行。如果我再次插入,listview只显示第一个插入的值总是eventhough。请帮我解决这个问题....
我的表创建和插入代码如下,供您参考
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>
数组列表表示如下
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;
}
答案 0 :(得分:0)
在方法insertUserMaster
中,您在插入之前删除所有数据。
删除此行:
SqlDB.delete("userMaster", null, null);
答案 1 :(得分:0)
使用正确的查询来避免这些问题。在插入之前检查是否要删除现有表..