I’m pretty sure this is a known issue and there is some info about this but I couldn’t find the right answer to it.
Basically, I have an SQLite table that I want the user to be able to remove and insert rows as he sees fit. The user can Insert rows and delete them but the problem is – if the user delete, lets say row 3 (position=2) then after this delete, he wont be able to insert new row there.
I get error 19 – #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
So, how can i delete a row and then rearrange the rows to be sequential without any gaps? Or otherwise, what is the best way to approach this?
Here is my code:
package android.GUI;
import android.GUI.R.string;
public class DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "Date";
public static final String KEY_DAY = "Day";
public static final String KEY_HOURS = "Hours";
public static final String KEY_START = "Start";
public static final String KEY_END = "End";
public static final String KEY_TOTAL_HOURS = "TotalH";
public static final String KEY_TOTAL_MINUTES = "TotalM";
private static final String TAG = "DBAdapter";
int dbSumHours;
int dbSumMinutes;
private static final String DATABASE_NAME = "shifts6";
private static final String DATABASE_TABLE = "newtimeDate6";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE = "create table newtimeDate6 (_id integer primary key autoincrement, "
+ "Date text not null, Hours text not null, TotalH text not null, TotalM text not null, Day text not null, Start text not null, End text not null )";
private final Context context;
private static DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
// opens the database
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// closes the database
public static void close() {
DBHelper.close();
}
// insert a Shift into the database
public long insertTitle(String Date, String Hour, int TotalH, int TotalM,
String Day, int ID, String Start, String End) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATE, Date);
initialValues.put(KEY_ROWID, ID);
initialValues.put(KEY_HOURS, Hour);
initialValues.put(KEY_TOTAL_HOURS, TotalH);
initialValues.put(KEY_TOTAL_MINUTES, TotalM);
initialValues.put(KEY_DAY, Day);
initialValues.put(KEY_START, Start);
initialValues.put(KEY_END, End);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public void addToDBTotal(String Date, String Hour, int TotalH, int TotalM,
String Day, int ID, String Start, String End) {
dbSumHours = dbSumHours + TotalH;
dbSumMinutes = dbSumMinutes + TotalM;
String hour = Hour;
String date = Date;
long id = insertTitle(Date, Hour, dbSumHours, dbSumMinutes, Day, ID,
Start, End);
}
// deletes a particular Shifts
public boolean deleteTitle(long rowId) {
Boolean deleted = db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
return deleted;
}
public void deleteAll() {
this.open();
this.db.delete(DATABASE_TABLE, null, null);
Main.C = 1;
}
// retrivs all titles
public Cursor getAllShifts() {
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_DATE,
KEY_HOURS, KEY_DAY, KEY_START, KEY_END }, null, null, null,
null, null);
}
public Cursor getSumHours() {
return db.query(DATABASE_TABLE, new String[] { KEY_TOTAL_MINUTES, KEY_TOTAL_HOURS },
null, null, null, null, null);
}
public static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + "to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS shifts");
onCreate(db);
}
}
}
You have to turn off autoincrement and then manage the rows and row id numbers yourself. After a delete you would have to read out each row after it, delete it and re-insert it in the previous row, repeat until you get to the end.
Now, why is it so important that there be no gaps?
And the best way to approach it is to not do it. Its extra cycles (gayety life) that is not gaining you anything.