I am using an array to send data to a SQLite database.
The array contains all the selected values.
private void addContacts(String[] selectedItems) {
manager.Insert_phone_contact(selectedItems);
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
My SQLite database code to insert the aforementioned array of "selectedItems" into the contentvalues looks like this:
public void Insert_phone_contact(String [] contact){
try{
SQLiteDatabase DB = this.getWritableDatabase();
for(int i=0;i<contact.length;i++){
ContentValues cv = new ContentValues();
cv.put(CONTACT_NAME, contact[i]);
DB.insert(TABLE_CONTACTS, null, cv);
DB.close();
}
}
catch(Exception ex){
Log.e("Error in phone contact insertion", ex.toString());
}
Only the first element of the array is stored in ContentValuescv, and not in all elements of the array.
What is wrong with this code? How can I insert all elements of an array into the table "TABLE_CONTACTS"?
Any help would be appreciated.
source
share