Android - content values โ€‹โ€‹over writing existing lines

I am trying to insert values โ€‹โ€‹using ContentValues , I inserted 5 values โ€‹โ€‹in 5 columns. After starting the application, I only have a line with the last set of ContentValues . The first four sets are not inserted.

  ContentValues cv = new ContentValues(); cv.put("name", "Cs Tech"); cv.put("name", "Wipro"); cv.put("name", "TCS"); cv.put("name", "Infosys"); cv.put("name", "Cognizant"); cv.put("mail", " joe@info.com "); cv.put("mail", " bru@wipro.com "); cv.put("mail", " jen@tcs.com "); cv.put("mail", " ram@infosys.com "); cv.put("mail", " cts@cts.com "); cv.put("contact", "180 151 2010"); cv.put("contact", "180 151 2011"); cv.put("contact", "180 151 2012"); cv.put("contact", "180 151 2013"); cv.put("contact", "180 151 2014"); cv.put("date", "27 Jul 2011"); cv.put("date", "27 Jul 2011"); cv.put("date", "27 Jul 2011"); cv.put("date", "27 Jul 2011"); cv.put("date", "27 Jul 2011"); this.db.insert(TABLE_NAME, "name", cv); 
+4
source share
3 answers

Itโ€™s easy to understand that ContentValues is some kind of hash table, which implies that this point of code

  cv.put("name", "Cs Tech"); cv.put("name", "Wipro"); cv.put("name", "TCS"); cv.put("name", "Infosys"); cv.put("name", "Cognizant"); 

eventually overwrites the value with the key = "name" 4 times, and the name finally gets the last value!

For this to work, you must do it sequentially as follows:

 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); 
+7
source

The best way to get closer to this is to insert all the column values โ€‹โ€‹and then continue with the next

For instance:

  cv.put("name", "gautam"); cv.put("id", "cse08119"); cv.put("country", "tamilnadu"); cv.put("city", "coimbatore"); cv.put("pin", "636213"); 

then now paste 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 until the loop reaches the end of the array!

+2
source
 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); 

you need to insert another 5 rows into the table. What you did is: put the values โ€‹โ€‹in ContentValues โ€‹โ€‹5 times. Therefore, it takes the last record before inserting into the table. You need to write 5 insert statements like this. I think now you will understand

0
source

All Articles