Android 数据库 rawQuery:奇怪的游标行为
Android database rawQuery: strange Cursor behaviour
我需要在升级之前检查我的数据库中的 tables。
我在自己的 DatabaseHelper 中覆盖了 SQLiteOpenHelper.onUpgrade(SQLiteDatabase db, int oldVer, int newVer)
,以便在数据库版本增加时完成工作。
我在 sqlite_master table 上使用此查询来获取 table 名称:select * from sqlite_master
但即使 cursor.getCount()==29
(数据库不是空并包含 29 个对象)
相同的查询在我的 APP 的其他地方效果很好。
我正在模拟 Android 4.4 (api 19) 上测试这个,而我的应用程序是用
构建的
minSdkVersion 12
targetSdkVersion 24
知道这种奇怪的行为吗?
代码如下:
public class DatabaseHelper extends SQLiteOpenHelper {
public static DatabaseHelper getHelper(Context context) {
if (instance == null)
synchronized( DatabaseHelper.class ) {
if( instance==null )
instance = new DatabaseHelper(context.getApplicationContext());
}
return instance;
}
private DatabaseHelper(Context c) {
super(c, DB_NAME, null, 6 /* version */);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
final boolean inTransaction = db.inTransaction();
if( !inTransaction )
db.beginTransaction();
Cursor cur = null;
try {
// cur = db.query("sqlite_master", null, null, null, null, null, null); // SAME BEHAVIOUR
cur = db.rawQuery("select * from sqlite_master", null );
int count = cur.getCount(); // ASSIGN 29 TO COUNT
while( cur.moveToNext()) {
** NEVER ENTERS HERE **
}
if( !inTransaction )
db.setTransactionSuccessful();
} catch (Exception e) {
log.e(DBTAG, e.getMessage(), e);
} finally {
if( cur!=null)
cur.close();
if( !inTransaction )
db.endTransaction();
}
}
}
我刚刚重构了你的案例,发现"sqlite"是保留给内部使用的,以下是我得到的ERROR信息。
android.database.sqlite.SQLiteException: object name reserved for internal use: sqlite_master
也许你可以尝试另一个 table 名称,看看是否可行。
我发布的代码在库模块中。
当我在主应用程序模块中移动它时,sqlite_master 上的查询有效!
我在 build.gradle 中将应用程序模块和库模块中的 buildToolsVersion 从 24.0.2 升级到 24.0.3。然后进行全面重建。
我不知道为什么但是...现在查询在主模块或库模块中都有效!
我需要在升级之前检查我的数据库中的 tables。
我在自己的 DatabaseHelper 中覆盖了 SQLiteOpenHelper.onUpgrade(SQLiteDatabase db, int oldVer, int newVer)
,以便在数据库版本增加时完成工作。
我在 sqlite_master table 上使用此查询来获取 table 名称:select * from sqlite_master
但即使 cursor.getCount()==29
(数据库不是空并包含 29 个对象)
相同的查询在我的 APP 的其他地方效果很好。
我正在模拟 Android 4.4 (api 19) 上测试这个,而我的应用程序是用
构建的minSdkVersion 12
targetSdkVersion 24
知道这种奇怪的行为吗?
代码如下:
public class DatabaseHelper extends SQLiteOpenHelper {
public static DatabaseHelper getHelper(Context context) {
if (instance == null)
synchronized( DatabaseHelper.class ) {
if( instance==null )
instance = new DatabaseHelper(context.getApplicationContext());
}
return instance;
}
private DatabaseHelper(Context c) {
super(c, DB_NAME, null, 6 /* version */);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
final boolean inTransaction = db.inTransaction();
if( !inTransaction )
db.beginTransaction();
Cursor cur = null;
try {
// cur = db.query("sqlite_master", null, null, null, null, null, null); // SAME BEHAVIOUR
cur = db.rawQuery("select * from sqlite_master", null );
int count = cur.getCount(); // ASSIGN 29 TO COUNT
while( cur.moveToNext()) {
** NEVER ENTERS HERE **
}
if( !inTransaction )
db.setTransactionSuccessful();
} catch (Exception e) {
log.e(DBTAG, e.getMessage(), e);
} finally {
if( cur!=null)
cur.close();
if( !inTransaction )
db.endTransaction();
}
}
}
我刚刚重构了你的案例,发现"sqlite"是保留给内部使用的,以下是我得到的ERROR信息。
android.database.sqlite.SQLiteException: object name reserved for internal use: sqlite_master
也许你可以尝试另一个 table 名称,看看是否可行。
我发布的代码在库模块中。 当我在主应用程序模块中移动它时,sqlite_master 上的查询有效!
我在 build.gradle 中将应用程序模块和库模块中的 buildToolsVersion 从 24.0.2 升级到 24.0.3。然后进行全面重建。
我不知道为什么但是...现在查询在主模块或库模块中都有效!