我很确定这是一个已知问题,并且有一些关于此的信息,但我找不到正确的答案。
基本上,我有一个 SQLite 表,我希望用户能够在他认为合适的时候删除和插入行。用户可以插入行并删除它们,但问题是 - 如果用户删除,让我们说第 3 行(位置 = 2),那么在删除之后,他将无法在那里插入新行。我收到错误 19 - #define SQLITE_CONSTRAINT 19 /* 由于违反约束而中止 */
那么,我怎样才能删除一行,然后将行重新排列为连续的而没有任何间隙?否则,解决此问题的最佳方法是什么?
这是我的代码:
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);
}
}
}