Nitin
Nitin

Reputation: 1986

How to add multiple rows in database in android?

I am developing an android application where i need to enter multiple rows in database at a one go, i have 18 rows and 4 columns in my user interface which are edit texts , is it possible to enter all data i data base with one button click , how can i do that ? Here is my code to generate one row

public void addRow(String rowStringOne, String rowStringTwo)
{
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);

// ask the database object to insert the new data
try{db.insert(TABLE_NAME, null, values);}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}

Upvotes: 1

Views: 2236

Answers (1)

Mojo Risin
Mojo Risin

Reputation: 8142

It depends from where you get the data. You have two possible scenarios.

  1. You have to insert records from other sql table - You can use single sql query that will fetch and insert the records. For more details read this post
  2. You have to insert records already loaded in memory - You can to implement some king of bulk insert. For more details how to use transactions and do bulk insert read this post

Upvotes: 1

Related Questions