나는 이것이 알려진 이슈이고 이것에 대한 정보가 있다고 확신하지만 그것에 대한 답을 찾을 수 없었다.일부 행을 삭제 한 후 테이블에 새 행을 삽입합니다. sqlite
기본적으로 사용자가 적합하다고 판단한 행을 제거하고 삽입 할 수있게하려는 SQLite 테이블이 있습니다. 사용자는 행을 삽입하고 삭제할 수 있지만 문제는 - 사용자가 삭제하면 3 행 (위치 = 2)을 말하게하고이 삭제 후에는 새로운 행을 삽입 할 수 없습니다. 오류가 발생합니다 - #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);
}
}
}
기본적으로 내가 직면하고있는 유일한 문제는 행을 삭제 한 후에 새 행을 추가 할 수 없다는 것입니다. 오류 19 메시지가 나타납니다. 그렇다면이 여분의 사이클없이 어떻게 행을 올바르게 추가 할 수 있습니까? 또한 테이블의 틈이 추가 공간을 차지하지 않습니까? – Yosi199
아니, 추가 공간이 없습니다. _id가 autoincrement이고 id를 말한 후에 id를 주려고하므로 오류가 발생합니다. 'KEY_ROWID'를 사용하여'insertTitle' 메쏘드에서 줄을 지우면 삽입이 작동합니다. – Barak
와우 당신은 훌륭합니다, 완벽하게 작동했습니다! 이제 cursor.getPosition()을 호출하면 간격이없는 행을 항상 순차적으로 얻을 수 있습니까? – Yosi199