user3169552
user3169552

Reputation: 107

Passing an array of data into a SQLite Database in android

I am using an array to send data to my 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 databse code to insert the above mentioned "selectedItems" array in contentvalues is as follows:

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 array item is stored in ContentValues cv, not all the array elements.
What's wrong in this code?
How can I insert all the array items in the "TABLE_CONTACTS" table?
Any help will be appreciated.

Upvotes: 4

Views: 11520

Answers (4)

MalhotraUrmil
MalhotraUrmil

Reputation: 86

you can using code i have just some changes your code.

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

    }

        cv.close();
        DB.close();
    }
catch(Exception ex){
    Log.e("Error in phone contact insertion", ex.toString());
}

Upvotes: 0

Pankaj Zanzane
Pankaj Zanzane

Reputation: 1242

You can also consider transactions in sqlite

    DB.beginTransaction();
    for(int i=0;i<contact.length;i++){    
        ContentValues cv = new ContentValues();  
        cv.put(CONTACT_NAME, contact[i]);
        DB.insert(TABLE_CONTACTS, "0.0", cv);
    }
    DB.setTransactionSuccessful();

    DB.endTransaction();

This will make insert operation faster than single insert

Upvotes: 2

DareDevil
DareDevil

Reputation: 5349

You have to fix few lines of code, First is to delete Db.Close() and add it after loop The code you were trying was closing the Db object after first insertion and unavailable for the rest of loop iterations.

public void Insert_phone_contact(String [] contact){
try{

    SQLiteDatabase DB = this.getWritableDatabase();
    ContentValues cv = new ContentValues(); //Declare once
    for(int i=0;i<contact.length;i++){            
        cv.put(CONTACT_NAME, contact[i]);
        DB.insert(TABLE_CONTACTS, null, cv); //Insert each time for loop count            
    }
    DB.close(); // Now close the DB Object
    }
catch(Exception ex){
    Log.e("Error in phone contact insertion", ex.toString());
}

Upvotes: 7

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

You will need to move insert and close statement outside for loop because currently you are storing only one value and not opening db again :

   ContentValues cv = new ContentValues();
    for(int i=0;i<contact.length;i++){
       // put all values in  ContentValues
        cv.put(CONTACT_NAME, contact[i]);
    }
    DB.insert(TABLE_CONTACTS, null, cv); // insert in db
    DB.close();  // call close

Upvotes: 3

Related Questions