How To Insert Data Into Sqlite Database Just One Time
Solution 1:
try Like this:
publicclassTestDatabaseHelperextendsSQLiteOpenHelper{
publicTestDatabaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@OverridepublicvoidonCreate(SQLiteDatabase db) {
// TODO Create Your tables here//and insert the pre loaded records here
}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Solution 2:
Assuming you're using SQLiteOpenHelper
to manage your database, put the inserts in the helper onCreate()
callback. It gets invoked exactly once when the database file is created for the first time. Just remember to call the database methods on the SQLiteDatabase
passed in as a parameter.
Solution 3:
you must check with a cycle "if" that is not already inserted that value in the database. I would do: 1) select fields from table 2)
if (field1 != value) then
execute(INSERT INTO TABLE_NAME VALUES(value1,value2,value3,...valueN));
else{
// continues with the program
}
3) close the connection
P.S. sorry for my bad english(I'm italian)
Solution 4:
Use sharedprefrence to check if data has been submitted before. Another workaround may be make a new table in sqlite and use that table to check if data have been inserted before. Eg. New table be check_insertion. With column id and user_id. Now check if in this table there is any entry if not then you may run your query which you want to be ran first time only.
Solution 5:
It's simple just create a static variable count and initialize it to 0. And increment counter to 1 after first insertion and also checks if counter is 0 before insertion. So in Your Case :
staticint count = 0;
if(count == 0){
mydb.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Candle stick 3',4000,'Solar garden / pathway light,Solar Panel:1pc crystal silicon solar cell,Material:Stainless steel ,WaterProof and safe ')");
count++;
}
Post a Comment for "How To Insert Data Into Sqlite Database Just One Time"