删除一些行后,在表中插入一个新行。源码

时间:2012-05-23 19:08:53

标签: android database sqlite

我很确定这是一个已知问题,有一些信息,但我找不到合适的答案。

基本上,我有一个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);

        }
    }

}

1 个答案:

答案 0 :(得分:1)

您必须关闭自动增量,然后自行管理行和行ID号。删除后,你必须读出它之后的每一行,删除它并重新插入前一行,重复直到你结束。

现在,为什么没有差距这么重要呢?

接近它的最好方法是不要这样做。它的额外周期(gayety life)并没有让你获得任何东西。