java - 在sqlite中移动一行(位置列)

标签 java android sqlite android-sqlite

所以,我目前正在使用 DragSortListView使用 SQLite 数据库。每当我将 ListView 的项目拖动到新位置时,我都想重新使用我的数据库。因此我编写了一段应该管理它的代码。但如果我将某些东西从较低的位置移动到较高的位置,它的排序就会很奇怪。我已经查看这段代码几个小时并询问了一些 friend ,但我从未找到解决方案。

代码

DatabaseHelper 中的方法

public void moveValues(int from, int to, String sammlung){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    System.out.println(from + " - " + to);
    if(from < to){
        //This part is working properly
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT ID FROM " + TABLE_NAME + " WHERE POSITION IS " + "\"" + from + "\" AND SAMMLUNG IS " + "\"" + sammlung + "\"", null);
        for (int i = from+1; i < to+1; i++){
            switchPositionValue(i, i-1);
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_5, to);
        cursor.moveToFirst();
        sqLiteDatabase.update(TABLE_NAME, contentValues, "ID=?", new String[]{cursor.getInt(0)+""});
        cursor.close();
    }else{
        //This is the not working part
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT ID FROM " + TABLE_NAME + " WHERE POSITION IS " + "\"" + from + "\" AND SAMMLUNG IS " + "\"" + sammlung + "\"", null);
        for (int i = from-1; i > to-1; i--){
            switchPositionValue(i, i+1);
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_5, to);
        cursor.moveToFirst();
        System.out.println(cursor.getInt(0));
        //The next line destroys it. But I dont know how to fix it or what exactly is not working
        sqLiteDatabase.update(TABLE_NAME, contentValues, "ID=?", new String[]{cursor.getInt(0)+""});
        cursor.close();
    }
}

参数为: from:它被带走的位置 to:它所到达的位置 sammlung:我将不同集合的所有项目保存在一个数据库中。所以它只是用来分类特定的项目。

更改特定位置行的值的方法

public void switchPositionValue(int before, int after){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_5, after);
    sqLiteDatabase.update(TABLE_NAME, contentValues, "POSITION=?", new String[]{before+""});
}

演示

Demonstration

因此,如果您知道为什么它的行为如此奇怪,或者确实知道在数据库中移动行的更好方法,我将非常感谢您的帮助。

最佳答案

Android 另一种方式 SQL one(COLUMN_5 是 POSITION 列)

public void moveValues(int from, int to) {
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    String query =
            "WITH p(from_position,to_position,max_position,min_position) AS (" +
                    "SELECT " +
                    "?," +
                    "?," +
                    "(SELECT max(" + COLUMN_5 + ") FROM " + TABLE_NAME + ")," +
                    "(SELECT min(" + COLUMN_5 + ") FROM " + TABLE_NAME + ")), " +

                    "updates AS(" +
                    "SELECT " + TABLE_NAME + ".id," + COLUMN_5 + " AS current, " + COLUMN_5 + " + 1 AS proposed " +
                    "FROM " + TABLE_NAME + " " +
                    "WHERE " + COLUMN_5 + " >= (SELECT to_position FROM p) " +
                    "AND " + COLUMN_5 + " < (SELECT from_position FROM p) " +

                    "UNION SELECT " + TABLE_NAME + ".id," + COLUMN_5 + " AS current, " + COLUMN_5 + " -1 AS proposed " +
                    "FROM " + TABLE_NAME + " " +
                    "WHERE " + COLUMN_5 + " <= (SELECT to_position FROM p) " +
                    "AND " + COLUMN_5 + " > (SELECT from_position FROM p) " +

                    "UNION SELECT " + TABLE_NAME + ".id," + COLUMN_5 + ",(SELECT to_position FROM p) " +
                    "FROM " + TABLE_NAME + " " +
                    "WHERE " + COLUMN_5 + " = (SELECT from_position FROM p) " +
                    "AND (SELECT from_position FROM p) <> (SELECT to_position FROM p)" +
                    "), " +

                    "finish_updates AS (" +
                    "SELECT * FROM updates " +
                    "WHERE max((SELECT from_position FROM p),(SELECT to_position FROM p)) <= (SELECT max_position FROM p) " +
                    "AND min((SELECT from_position FROM p),(SELECT to_position FROM p)) >= (SELECT min_position FROM p)" +
                    ")" +

                    "UPDATE " + TABLE_NAME + " SET " + COLUMN_5 + " = " +
                    "(" +
                    " SELECT proposed " +
                    " FROM finish_updates " +
                    " WHERE finish_updates.id = " + TABLE_NAME + ".id" +
                    ") " +
                    "WHERE " + TABLE_NAME + ".id IN " +
                    "(" +
                    " SELECT id FROM finish_updates" +
                    ");";
    SQLiteStatement stmnt = sqLiteDatabase.compileStatement(query);
    stmnt.bindLong(1,from);
    stmnt.bindLong(2,to);
    stmnt.executeUpdateDelete();
}

fiddle test SQL at

关于java - 在sqlite中移动一行(位置列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59849696/

相关文章:

Java 证书客户端 SSL : unable to find valid certification path to requested target

android - 使用 Android Studio Google Maps Activity 不显示 map

android - SQL 占位符 ("?")在 SQLiteDatabae.update 中不起作用

c# - 将 SQLite 数据填充到 UWP 应用程序中

java - 多对多关系返回空集合

java - 如何优化此递归回溯算法?

java - 在 Jersey 1.18.1 请求过滤器中获取资源注释

java - 与自定义字体相关的 Eclipse LogCat 错误

javascript - 通过自定义 Cordova 方案获得安全来源

sql - LIMIT 1的替代方案,以提取日期范围以及前后的第一个条目?