I have seen many questions on stackoverflow but none of the solutions work. I want to add only 1 row in the database at the time on its creation and the only thing that the app does is update this row with new values.
I have tried writing an INSERT statement in the SQLiteOpenHelper class but it does not work. No row is inserted.
Code:
public class db_manager extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "tally.db";
private static final String TABLE_TALLY = "tally";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_VALUE = "value";
public db_manager(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_TALLY + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_VALUE + " TEXT " +
");";
db.execSQL(query);
db.execSQL("INSERT INTO tally(value) VALUES (100)");
}
But if I try to insert a row from the mainActivity class using a method that I declared in the SQLiteOpenHelper class the row is inserted successfully but it add a new row every time I run the app..
Code:
// In mainActivity
DM = new db_manager(this,null,null,1);
DM.WriteFirst(10);
//In SQLiteOpenHelper class
public void WriteFirst(int value){
ContentValues values = new ContentValues();
values.put(COLUMN_VALUE, value);
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_TALLY, null, values);
db.close();
}
I want to INSERT the row only once.