房间迁移 - 在迁移代码中使用光标(获取行ID)是否安全?
基本上重命名,然后根据行ID 更新一些值 其中isnext = 1
。
我想我需要使用光标
,如此答案。因此,我的自动迁移的代码看起来像这样:
// For Auto-Migration
@RenameColumn(tableName = "Notifications", fromColumnName = "isNext", toColumnName = "wasShown")
static class MyAutoMigration implements AutoMigrationSpec {
@Override
public void onPostMigrate(@NonNull SupportSQLiteDatabase database) {
// Invoked once auto migration is done
Cursor cursor = database.query("SELECT * from Notifications WHERE wasShown = 1");
String row_id = String.valueOf(cursor.getPosition());
database.execSQL("UPDATE Notifications SET wasShown = 1 WHERE id < ?", new String[]{row_id});
database.execSQL("UPDATE Notifications SET wasShown = 0 WHERE id = ?", new String[]{row_id});
cursor.close();
}
}
在某种程度上使用这种逻辑在迁移代码中使用这种逻辑?
例如,因为它可能会抛出异常,或者发生其他事情会崩溃。
我以前从未通过老式的光标直接处理过SQL操纵,因此我的印象可能是脆弱的。
Basically rename and then update some values based on the row id where isNext = 1
.
I suppose I need to use a Cursor
as shown in this answer. So my code with auto-migration would look like this:
// For Auto-Migration
@RenameColumn(tableName = "Notifications", fromColumnName = "isNext", toColumnName = "wasShown")
static class MyAutoMigration implements AutoMigrationSpec {
@Override
public void onPostMigrate(@NonNull SupportSQLiteDatabase database) {
// Invoked once auto migration is done
Cursor cursor = database.query("SELECT * from Notifications WHERE wasShown = 1");
String row_id = String.valueOf(cursor.getPosition());
database.execSQL("UPDATE Notifications SET wasShown = 1 WHERE id < ?", new String[]{row_id});
database.execSQL("UPDATE Notifications SET wasShown = 0 WHERE id = ?", new String[]{row_id});
cursor.close();
}
}
Is it in some way "risky" to use such logic in migration code?
For example because it might throw exceptions, or something else happens that would crash the app.
I never worked with direct SQL manipulation through old-school cursors before, so I have the impression this logic might be fragile.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该没有问题。但是,没有必要,因为无需光标可以进行更新,因为更新可以确定值。
例如,SQL可能是: -
以下说明了以上: -
上更高的查询结果: -
post-date查询结果: -
sqlfiddle.com/#!5/9eecb7/15190“ rel =“ nofollow noreferrer”>这是一个sqlfiddle demo
您的代码可能是: -
There should be no issues. However, there is no need as the UPDATEs could be done without the need for a Cursor as the UPDATE could determine the value.
e.g. the SQL could be:-
The following demonstrates the above :-
The pre-update query results in :-
The post-date query results in :-
Here's an SQLFiddle Demo
You code could then be :-
我不得不适应Miket的好答案,以便在迁移中又有一个用例:
当
isnext = 1
剩下的没有值时,新表中的所有值都必须设置为
washown = 1
。经过一些反复试验,我进行了以下有条件的逻辑工作,以涵盖我的所有情况:
I had to adapt MikeT's great answer to fit one more use-case in my migration:
When there was no value with
isNext=1
left in the old table, all values in the new table had to be set towasShown=1
.After some trial and error I made the following conditional logic work to cover all of my cases: