Passing an array of data into a SQLite Database in

2020-02-29 06:18发布

问题:

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.

回答1:

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


回答2:

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


回答3:

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



回答4:

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