0

I wanted to insert data into my SQLite database. It consists of 3 columns: name, family, id.

This is my method:

private void FillColleagueTable(){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_NAME, "mack"); 
    values.put(KEY_FAMILY, " jasone"); 

    // Inserting Row
     db.insert(TABLE_COLLEAGUE, null, values);
    //db.close(); // Closing database connection

}

I want to add lots of rows to my table, I think this method is not the best way, is there any way to use something like query in my database handler to reduce the line of my code?any help would be appreciated.

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
Ladan Nekuii
  • 185
  • 1
  • 6
  • 18

2 Answers2

0
  1. Use For Loop and Android's default db.insert

    ContentValues values = new ContentValues(); for(int i = 0; i<=5; i++) { values.put(COLUMN_NAME, i); values.put(COLUMN_NAME, 0); db.insert(TABLE_NAME, null, values); }

  2. Use union combined with multiple selects in insert query as:

    db.execSQL("INSERT INTO "+TABLE_NAME+ "SELECT 'mack' as KEY_NAME, 'jasone' AS KEY_FAMILY union select 'mack1', 'jasone1' union select 'mack2', 'jasone2'" );

    INSERT QUERY to have Following syntax:

    INSERT INTO 'tablename' SELECT 'data1' AS 'column1', 'data2' AS 'column2' UNION SELECT 'data3', 'data4' UNION SELECT 'data5', 'data6' UNION SELECT 'data7', 'data8'

  3. Use transactions as here

Community
  • 1
  • 1
ngrashia
  • 9,869
  • 5
  • 43
  • 58
0

You can use the following code:

String sql = "INSERT or replace INTO tbl_Contain (name, family, id) VALUES('asdf','asdf,'1')" ;       
db.execSQL(sql);
Solenya
  • 694
  • 6
  • 21
  • thank's it's working for me.but in another way:String sql = "INSERT INTO staff ( department_id,job_id,colleague_id) SELECT '1','1','1' UNION SELECT '1','1','2' " ; – Ladan Nekuii Jul 09 '14 at 13:51