Android - Content Values Over Writing Existing Rows
Solution 1:
It is not hard to understand that ContentValues
is some kind of a hashtable which implies that this peice of code
cv.put("name", "Cs Tech");
cv.put("name", "Wipro");
cv.put("name", "TCS");
cv.put("name", "Infosys");
cv.put("name", "Cognizant");
is ultimately overwriting the Value with key="name" 4 times and name is finally getting the last value!
For this to work, you should do it sequentially like this:
ContentValues cv = new ContentValues();
cv.put("name", "Cs Tech");
cv.put("mail", "joe@info.com");
cv.put("contact", "180 151 2010");
cv.put("date", "27 Jul 2011");
this.db.insert(TABLE_NAME, "name", cv);
cv.put("name", "Wipro");
cv.put("mail", "bru@wipro.com");
cv.put("contact", "180 151 2011");
cv.put("date", "27 Jul 2011");
this.db.insert(TABLE_NAME, "name", cv);
cv.put("name", "TCS");
cv.put("mail", "jen@tcs.com");
cv.put("contact", "180 151 2012");
cv.put("date", "27 Jul 2011");
this.db.insert(TABLE_NAME, "name", cv);
cv.put("name", "Infosys");
cv.put("mail", "ram@infosys.com");
cv.put("contact", "180 151 2013");
cv.put("date", "27 Jul 2011");
this.db.insert(TABLE_NAME, "name", cv);
cv.put("name", "Cognizant");
cv.put("mail", "cts@cts.com");
cv.put("contact", "180 151 2014");
cv.put("date", "27 Jul 2011");
this.db.insert(TABLE_NAME, "name", cv);
Solution 2:
The best way to approach to this is, insert all the values of the columns, and then continue with the next
Eg:
cv.put("name", "gautam");
cv.put("id", "cse08119");
cv.put("country", "tamilnadu");
cv.put("city", "coimbatore");
cv.put("pin", "636213");
then now inset the values..
You can use a while loop and an array...
eg:
cv.put("name",namearray[i]);
cv.put("id",id[i]);
cv.put("country", countryname[i]);
cv.put("city",countryarray[i]);
cv.put("pin", pincode[i]);
do while the loop till it reaches the end of the array!
Solution 3:
ContentValuescv=newContentValues();
cv.put("name", "Cs Tech");
cv.put("mail", "joe@info.com");
cv.put("contact", "180 151 2010");
cv.put("date", "27 Jul 2011");
this.db.insert(TABLE_NAME, "name", cv);
like this you have to insert another 5 rows in your table.What you did is--putting values in ContentValues 5 times.So it takes the last entry before inserting in the table.You have to write 5 insert statements like this.I think now you will understand
Post a Comment for "Android - Content Values Over Writing Existing Rows"