java - SQLITE 数据库中请求的索引 0 大小为 0 错误

标签 java android sqlite android-cursor

数据库中有数据(准确地说是 2 行),每行都有信息。

这是来自 DBADAPTER 的代码(很重要,不是全部):

// Field Names:
    public static final String KEY_ROWID = "_id";
    public static final String KEY_DEVICE = "device";
    public static final String KEY_TYPE = "type";
    public static final String KEY_DEVID = "devid";

    public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_DEVICE, KEY_TYPE, KEY_DEVID};
    public static final String[] DEVID_KEY = new String[] {KEY_DEVID};

    // Column Numbers for each Field Name:
    public static final int COL_ROWID = 0;
    public static final int COL_DEVICE = 1;
    public static final int COL_TYPE = 2;
    public static final int COL_DEVID = 3;

    // DataBase info:
    public static final String DATABASE_NAME = "dbDevices";
    public static final String DATABASE_TABLE = "mainDevices";
    public static final int DATABASE_VERSION = 10; // The version number must be incremented each time a change to DB structure occurs.

    //SQL statement to create database
    private static final String DATABASE_CREATE_SQL = 
            "CREATE TABLE " + DATABASE_TABLE 
            + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_DEVICE + " TEXT NOT NULL, "
            + KEY_TYPE + " TEXT, "
            + KEY_DEVID + " TEXT NOT NULL "
            + ");";

    private final Context context;
    private DatabaseHelper myDBHelper;
    private SQLiteDatabase db;

    // Get a specific row (by devid)
        public String getDevName(String devid) {
            String name;
            String where = KEY_DEVID + " like '%" + devid + "%'";
            Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                            where, null, null, null, null, null);
                c.moveToFirst();
                name = c.getString(COL_DEVICE);
            return name;
        }

这是调用它的代码:

String incomingMessage = " ";
incomingMessage = in.readLine() + System.getProperty("line.separator");
devName = devDB.getDevName(incomingMessage);

我知道这是在获取信息,因为它记录了:
03-16 15:39:47.072: V/String(18073): 5122

每当我运行它时,我都会收到错误:

03-16 15:39:47.122: W/System.err(18073): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
03-16 15:39:47.122: W/System.err(18073):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
03-16 15:39:47.122: W/System.err(18073):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
03-16 15:39:47.132: W/System.err(18073):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
03-16 15:39:47.132: W/System.err(18073):    at com.ti.cc3x.android.DBAdapter.getDevName(DBAdapter.java:121)
03-16 15:39:47.132: W/System.err(18073):    at com.ti.cc3x.android.broadcastListener$1.run(broadcastListener.java:112)
03-16 15:39:47.132: W/System.err(18073):    at java.lang.Thread.run(Thread.java:841)

我试过检查非空游标。所做的只是什么都不告诉我。我知道我要求它查看的地方有数据,我只是不知道为什么它不显示。谁能告诉我哪里出错了?谢谢!

最佳答案

你在使用 SQLiteOpenHelper 吗?如果是这样,您需要将数据库变量 db 设置为您的数据库 before 尝试像这样读取或写入它:

db = this.getWritableDatabase();

...您的新 getDevName(..) 方法应如下所示:

public String getDevName(String devid) {
    db = this.getWritableDatabase();

    String name;
    String where = KEY_DEVID + " like '%" + devid + "%'";
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    c.moveToFirst();
    name = c.getString(COL_DEVICE);

    db.close(); // close your db connection when you're done with it

    return name;
}

来自 Android 开发者文档:

public SQLiteDatabase getWritableDatabase () - The first time this is called, the database will be opened and onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and/or onOpen(SQLiteDatabase) will be called. Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to call close() when you no longer need the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed.

关于java - SQLITE 数据库中请求的索引 0 大小为 0 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29085666/

相关文章:

java - switch语句客户端问题

java - 如何更改操作栏选项菜单项标题的字体?

android - Gradle 构建期间序言中不允许出现内容

android - 使用 alternativeIdePath 在 Intellij 上创建插件时找不到 Android Studio

c++ - 错误 C2440 : 'initializing' : cannot convert from

java - 我在添加数组的相同索引时得到空值

java - Vaadin,子弹出窗口打开时禁用父窗口?

java - 为什么在填充包含 < 类型元素的集合时行为会有所不同?扩展 T>?

sql - SQLite和范围查询

sqlite - SQLite 中的 SAVEPOINT 机制