我正在使用ORMLite来查询我的数据库。我有下面的代码。它在我尝试的每部手机上都能正常工作(Nexus 4,5,S3)。但是,它在HTC One和MotoX上出错。
try {
return Globals.getApplication().getDBHelper().getUserDao().queryForId(id);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
这是User Dao&类:
public Dao<User, String> getUserDao() throws SQLException {
if (userDao == null) {
userDao = getDao(User.class);
}
return userDao;
}
@DatabaseTable(tableName = User.tableName)
public class User {
public static final String tableName = "User";
private static final String TAG = "[User]";
@DatabaseField(id=true)
private String id;
@DatabaseField(columnName = "name")
private String name;
@DatabaseField(columnName = "phone", canBeNull = true)
private String phone;
}
错误如下。当它进入这种状态时,即使我重新启动应用程序,它也会继续崩溃。
有关为什么它只发生在特定手机上的任何想法?或是什么原因导致数据库处于不良状态?
AndroidRuntime E FATAL EXCEPTION: main
E java.lang.RuntimeException: Unable to start activity ComponentInfo{com.leo.android/com.leo.android.ui.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 4 from CursorWind
ow. Make sure the Cursor is initialized correctly before accessing data from it.
E at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
E at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
E at android.app.ActivityThread.access$600(ActivityThread.java:162)
E at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
E at android.os.Handler.dispatchMessage(Handler.java:99)
E at android.os.Looper.loop(Looper.java:158)
E at android.app.ActivityThread.main(ActivityThread.java:5751)
E at java.lang.reflect.Method.invokeNative(Native Method)
E at java.lang.reflect.Method.invoke(Method.java:511)
E at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
E at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
E at dalvik.system.NativeStart.main(Native Method)
E Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 4 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
E at android.database.CursorWindow.nativeGetLong(Native Method)
E at android.database.CursorWindow.getLong(CursorWindow.java:565)
E at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
E at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:220)
E at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
E at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:163)
E at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:57)
E at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55)
E at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:245)
E at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:194)
E at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:263)
E at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:361)
E at com.leo.android.entities.User.getUser(User.java:235)
答案 0 :(得分:2)
在查询位置使用以下代码从数据库中获取所有数据
for ex:-
select * from tableName where name = "a";