简体   繁体   中英

How to insert data into SQLite database in android?

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???

   //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!

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

SQLite Databases are persistent, so once you have the app on a phone or tablet and the DB is created, the records should stay in there until you delete the records or the whole database.

You should be calling the SQLiteDatabaseHelper from your Application class. So every time you open the app, it just looks for the database by the name you give it. If it can't find the database it creates it - If you have a record that you want inserted once and only once, you should create the records in the onCreate method.

Without code or more detail on exactly what you want to do, it's hard to help too much. What you can do though is tie your sql insert function to an OnClickListener or some other action listener so the entry is only added when you want and not each time the activity is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM