5

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

1
  • thanq all for your kind help :-) now i got a better understanding of SQLite :-) Commented Jun 14, 2012 at 6:50

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Consider wrapping database operations in transaction - see android example updating database using a transaction
5

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

1 Comment

This is good to insert data to fetch database with initial data so isn't empty when first installed the app.
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.