当我升级数据库时,数据库的版本号会更新,但是当查询被触发时,前两次会返回较旧的游标列。
对于前 迁移前的列数:16 迁移后预期的列数:21
但是,在运行迁移之后 数据库版本号已更新,但第一个查询的列数为16 然后在下一次查询或应用重启时更新为21。
有人知道为什么会这样吗?
以下是相同的模拟代码
public class MainActivity extends AppCompatActivity {
DbHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Stetho.initializeWithDefaults(this);
helper = new DbHelper(this);
SQLiteDatabase database = helper.getWritableDatabase();
database.execSQL("CREATE TABLE mytable (names TEXT, co TEXT)");
Cursor c1 = database.rawQuery("SELECT * FROM mytable", null);
Log.d("rajas", "Count - " + c1.getColumnCount());
c1.close();
database.execSQL("ALTER TABLE mytable ADD COLUMN pass TEXT");
Cursor c2 = database.rawQuery("SELECT * FROM mytable", null);
Log.d("rajas", "Count - " + c2.getColumnCount());
}
class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, "dbdbb", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
在上面的代码中,迁移前后的计数将保持为2。
答案 0 :(得分:1)
这似乎是一个错误。
有很多可以解决的问题:-
例如,请考虑:-
helper = new DbHelper(this);
SQLiteDatabase database = helper.getWritableDatabase();
database.execSQL("CREATE TABLE mytable (names TEXT, co TEXT)");
Cursor c1 = database.rawQuery("SELECT * FROM mytable", null);
Log.d("rajas", "Count - " + c1.getColumnCount() + " Alt count = " + (c1.getColumnNames()).length);
c1.close();
Cursor ti = database.rawQuery("PRAGMA table_info(mytable);",null);
DatabaseUtils.dumpCursor(ti);
database.execSQL("ALTER TABLE mytable ADD COLUMN pass TEXT");
Cursor c2 = database.rawQuery("SELECT * FROM mytable", null);
Log.d("rajas", "Count - " + c2.getColumnCount() + " Alt count = " + (c2.getColumnNames()).length);
ti = database.rawQuery("PRAGMA table_info(mytable);",null);
DatabaseUtils.dumpCursor(ti);
ti.close();
Cursor c3 = database.rawQuery("SELECT names,co,pass FROM mytable", null);
Log.d("rajas", "Count - " + c3.getColumnCount() + " Alt count = " + (c3.getColumnNames()).length);
ti = database.rawQuery("PRAGMA table_info(mytable);",null);
DatabaseUtils.dumpCursor(ti);
c3.close();
ti.close();
database.close();
database = helper.getWritableDatabase();
Cursor c4 = database.rawQuery("SELECT * FROM mytable", null);
Log.d("rajas", "Count - " + c4.getColumnCount() + " Alt count = " + (c4.getColumnNames()).length);
c4.close();
结果为:-
2019-12-03 07:48:23.484 D/rajas: Count - 2 Alt count = 2
2019-12-03 07:48:23.485 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5df21a1
2019-12-03 07:48:23.486 I/System.out: 0 {
2019-12-03 07:48:23.486 I/System.out: cid=0
2019-12-03 07:48:23.486 I/System.out: name=names
2019-12-03 07:48:23.486 I/System.out: type=TEXT
2019-12-03 07:48:23.486 I/System.out: notnull=0
2019-12-03 07:48:23.486 I/System.out: dflt_value=null
2019-12-03 07:48:23.486 I/System.out: pk=0
2019-12-03 07:48:23.486 I/System.out: }
2019-12-03 07:48:23.486 I/System.out: 1 {
2019-12-03 07:48:23.486 I/System.out: cid=1
2019-12-03 07:48:23.486 I/System.out: name=co
2019-12-03 07:48:23.486 I/System.out: type=TEXT
2019-12-03 07:48:23.486 I/System.out: notnull=0
2019-12-03 07:48:23.486 I/System.out: dflt_value=null
2019-12-03 07:48:23.486 I/System.out: pk=0
2019-12-03 07:48:23.486 I/System.out: }
2019-12-03 07:48:23.486 I/System.out: <<<<<
2019-12-03 07:48:23.491 D/rajas: Count - 2 Alt count = 2
2019-12-03 07:48:23.491 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@fc68fc6
2019-12-03 07:48:23.491 I/System.out: 0 {
2019-12-03 07:48:23.491 I/System.out: cid=0
2019-12-03 07:48:23.491 I/System.out: name=names
2019-12-03 07:48:23.492 I/System.out: type=TEXT
2019-12-03 07:48:23.492 I/System.out: notnull=0
2019-12-03 07:48:23.492 I/System.out: dflt_value=null
2019-12-03 07:48:23.492 I/System.out: pk=0
2019-12-03 07:48:23.492 I/System.out: }
2019-12-03 07:48:23.492 I/System.out: 1 {
2019-12-03 07:48:23.492 I/System.out: cid=1
2019-12-03 07:48:23.492 I/System.out: name=co
2019-12-03 07:48:23.492 I/System.out: type=TEXT
2019-12-03 07:48:23.492 I/System.out: notnull=0
2019-12-03 07:48:23.492 I/System.out: dflt_value=null
2019-12-03 07:48:23.492 I/System.out: pk=0
2019-12-03 07:48:23.492 I/System.out: }
2019-12-03 07:48:23.492 I/System.out: 2 {
2019-12-03 07:48:23.492 I/System.out: cid=2
2019-12-03 07:48:23.492 I/System.out: name=pass
2019-12-03 07:48:23.492 I/System.out: type=TEXT
2019-12-03 07:48:23.492 I/System.out: notnull=0
2019-12-03 07:48:23.492 I/System.out: dflt_value=null
2019-12-03 07:48:23.492 I/System.out: pk=0
2019-12-03 07:48:23.492 I/System.out: }
2019-12-03 07:48:23.493 I/System.out: <<<<<
2019-12-03 07:48:23.493 D/rajas: Count - 3 Alt count = 3
2019-12-03 07:48:23.493 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@7dbb87
2019-12-03 07:48:23.494 I/System.out: 0 {
2019-12-03 07:48:23.494 I/System.out: cid=0
2019-12-03 07:48:23.494 I/System.out: name=names
2019-12-03 07:48:23.494 I/System.out: type=TEXT
2019-12-03 07:48:23.494 I/System.out: notnull=0
2019-12-03 07:48:23.494 I/System.out: dflt_value=null
2019-12-03 07:48:23.494 I/System.out: pk=0
2019-12-03 07:48:23.494 I/System.out: }
2019-12-03 07:48:23.494 I/System.out: 1 {
2019-12-03 07:48:23.494 I/System.out: cid=1
2019-12-03 07:48:23.494 I/System.out: name=co
2019-12-03 07:48:23.494 I/System.out: type=TEXT
2019-12-03 07:48:23.494 I/System.out: notnull=0
2019-12-03 07:48:23.494 I/System.out: dflt_value=null
2019-12-03 07:48:23.494 I/System.out: pk=0
2019-12-03 07:48:23.494 I/System.out: }
2019-12-03 07:48:23.495 I/System.out: 2 {
2019-12-03 07:48:23.495 I/System.out: cid=2
2019-12-03 07:48:23.495 I/System.out: name=pass
2019-12-03 07:48:23.495 I/System.out: type=TEXT
2019-12-03 07:48:23.495 I/System.out: notnull=0
2019-12-03 07:48:23.495 I/System.out: dflt_value=null
2019-12-03 07:48:23.495 I/System.out: pk=0
2019-12-03 07:48:23.495 I/System.out: }
2019-12-03 07:48:23.495 I/System.out: <<<<<
2019-12-03 07:48:23.498 D/rajas: Count - 3 Alt count = 3