vendredi 10 juillet 2015

Android Creating SQLite Database and Inserting Rows - Best practise?

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')");
        // ........

    }


}

Aucun commentaire:

Enregistrer un commentaire