0

I am using SQLLite Database supplied by Android.

I have a Table myTable with two coloumns: col_id and colName. The gettersetter class is as follows:

 public class MyTable{

        private long id;
    private String colName;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getColName() {
        return colName;
    }

    public void setColName(String colName) {
        this.colName = colName;
    }

}

I have a method in database connector which inserts a single row :

 public void insertMyTable(String colName)
           {
              ContentValues newCon = new ContentValues();
              newCon.put("colName", colName);
              open();
              database.insert("myTable", null, newCon);
              close();
           }

Is there a way to insert a List of objects of type MyTable into the database, i have over 100 records to be inserted at once.

Thanks in advance

1 Answer 1

4
public void insertMyTable(List<MyTable> myTables)
{
    open();
    database.beginTransaction();
    for (MyTable myTable : myTables)
    {
        ContentValues insertValues = new ContentValues();
        insertValues.put("colName", myTable.getColName());
        database.insert("myTable", null, insertValues);
    }  
    database.setTransactionSuccessful();
    database.endTransaction();
    close();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Nguyen for the response. However i was looking to use the gettersetter class to insert the value. Anyway to inser a single or multiple value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.