Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert data into SQLite database in android?

Tags:

android

sqlite

I already saw an example of using SQlite in android...but in that particular example, records are inserted each time the app is ran..But i wanted to insert it only once and permanently! any possibilities???

like image 969
rampireram Avatar asked Jun 13 '12 17:06

rampireram


People also ask

Is SQLite still used in Android?

SQLite is another data storage available in Android where we can store data in the user's device and can use it any time when required.

Which of the following is used to insert a data into the database in Android?

Which of the following is used to insert a data into the database in Android? Android SQLite is the most preferred method of storing data in any android applications. Android SQLite is a relational database, therefore, It supports all types of relational database features.


2 Answers

   //DB HANDLER CLASS
    public void saveRecords(String info, int otherinfo, int greatinfo){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(STRING_COLUMNA, info); // inserting a string
    values.put(STRING_COLUMNB, otherinfo); // inserting an int
    values.put(STRING_COLUMNC, greatinfo); // inserting an int

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

}

Good luck!

like image 88
KDEx Avatar answered Sep 26 '22 18:09

KDEx


Insert values in onCreate method of your SQLiteOpenHelper. It called only when your DB had been created.

like image 38
Dmitry Zaytsev Avatar answered Sep 23 '22 18:09

Dmitry Zaytsev