2

in my Application I am using an SQLite Database. This Database get's created by a DBHelper-class, if it isn't created yet.

Initially I need a table with about 30 Rows. What is the best practice, to fill it with data? Here is my code. It works nicely, but I think there is a better way of doing it?

public class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context){
        super(context, DATABASE_NAME , null, 1);
    }

    private static final String DATABASE_TABLE_QUOTES = "quotes";


    public void onCreate(SQLiteDatabase db) {



        String CREATE_TABLE_QUOTES = "CREATE TABLE IF NOT EXISTS `" + DATABASE_TABLE_QUOTES + "` (\n" +
        "\t`quote_id`\tINTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
        "\t`quote`\tTEXT NOT NULL\n" +
        ");";

        db.execSQL(CREATE_TABLE_QUOTES);


        db.execSQL("INSERT INTO " + DATABASE_TABLE_QUOTES + "VALUES (1, 'test 1')");
        db.execSQL("INSERT INTO " + DATABASE_TABLE_QUOTES + "VALUES (2, 'test 2')");
        db.execSQL("INSERT INTO " + DATABASE_TABLE_QUOTES + "VALUES (3, 'test 3')");
        db.execSQL("INSERT INTO " + DATABASE_TABLE_QUOTES + "VALUES (4, 'test 4')");
        // ........

    }


}
Michael B
  • 1,579
  • 3
  • 22
  • 48
  • Refer this tutorial example for best practice : http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/ – SANAT Jul 10 '15 at 12:44

2 Answers2

3

You can try the following method.

ContentValues values=new ContentValues();
            values.put(1, "test1");
            db.insert("table_name", null, values);
            values.put(2, "test2");
            db.insert("table_name", null, values);
            values.put(3,"test3");
            db.insert("table_name", null, values);
            values.put(4,"test4");
            db.insert("table_name", null, values);
            ...

for more details check these links

http://mrbool.com/how-to-insert-data-into-a-sqlite-database-in-android/28895

How to insert value in data base using sqlite in android?

Community
  • 1
  • 1
Anusha Harish
  • 376
  • 3
  • 14
  • Thank You for the links. Are you sure about your solution? values.put expects Parameter 1 = String key – Michael B Jul 10 '15 at 09:16
  • Ya.. You can also follow this link. http://stackoverflow.com/questions/6925707/android-content-values-over-writing-existing-rows . Where multiple rows are being inserted.. Hope this helps :) – Anusha Harish Jul 10 '15 at 09:29
2

Since you have asked for best practice, I am referencing this answer You should use statements

database.beginTransaction();
SQLiteStatement stmt = database.compileStatement(sql);

for (int i = 0; i < NUMBER_OF_ROWS; i++) {
    //generate some values

    stmt.bindString(1, randomName);
    stmt.bindString(2, randomDescription);
    stmt.bindDouble(3, randomPrice);
    stmt.bindLong(4, randomNumber);

    long entryID = stmt.executeInsert();
    stmt.clearBindings();
}

database.setTransactionSuccessful();
database.endTransaction();

Reference

Here is one more link where a comparison between these two insertion methods is given. (for 100 records, normal insertion took 1657ms, while statements with endTransaction() took 92ms)

Community
  • 1
  • 1
Darpan
  • 5,193
  • 3
  • 45
  • 74