Android SQLite查询问题

发布于 2024-12-01 15:25:24 字数 2264 浏览 0 评论 0原文

我正在学习如何在 Android 上使用数据库,我正在尝试一个简单的示例,其中我有两个文本框 [brakes 和 rpm],当我在这些框中输入值并点击“写入按钮”时,它将它们输入到数据库表“性能”中”。当我点击“读取”按钮时,它应该查询表并打印内容,但查询时出现以下错误:

android.database.CursorIndexOutOfBoundsException:索引 -1 re 询问,大小为 0

这是我的相关代码:

private static final String KEY_ROWID = "_id";
private static final String KEY_BRAKES = "brakes";
private static final String KEY_RPM="rpm";
private static final String DATABASE_NAME = "MyDB2";
private static final String DATABASE_TABLE = "performance";
private static final int DATABASE_VERSION=1;
private static final String DATABASE_CREATE = "create table performance (_id integer primary key autoincrement, " + "brakes text not null, rpm text not null);";


    //--insert an entry to the database--
public void insertData(String filter, String data){
    ContentValues entry = new ContentValues();

    if(filter.equals("b")){
        entry.put(KEY_BRAKES, data);
        Log.d(TAG,"inserted brake data");
        db.insert(DATABASE_TABLE, null, entry);
    }
    else if (filter.equals("r")){
        entry.put(KEY_RPM, data);
        Log.d(TAG,"inserted rpm data");
        db.insert(DATABASE_TABLE, null, entry);
    }


}

    //--retrive all entries
public Cursor getAllData(){
    Log.d(TAG,"about to query");
    try{
    Cursor c = db.query(DATABASE_TABLE, new String[]{KEY_BRAKES,KEY_RPM}, null, null, null, null, null);
    Log.d(TAG,"id=" + c.getString(0));

    return c;
    }catch(Exception e){
        Log.d (TAG,"error");
        Log.e(TAG,e.toString());
    }

    return null;
}

我输入项目的方式如下:

brakeRead = brakes.getText().toString();
rpmRead = brakes.getText().toString();
db.open();
db.insertData("b", brakeRead);
db.insertData("r", rpmRead);
db.close();

我查询的方式如下:

                db.open();
            Cursor c = db.getAllData();
            if(c.moveToFirst())
            {
                do{
                    output.append("id: " +  c.getString(0) + " brakes: " + c.getString(1) + " rpm: " + c.getString(2));
                }while(c.moveToNext());
            }
            db.close();

但我在查询时收到此错误: android.database.CursorIndexOutOfBoundsException:Index - 1 重新 已询问,大小为 0

谁能看看我是否做错了什么?

I am learning how to uses databases on Android and i am trying a simple example in which i have two textboxes [brakes and rpm] when i enter values into those boxes and hit the "write button" it enters them into the database table "performance". When i hit the "read" button it should query the table and print the contents but i get the following error while querying:

android.database.CursorIndexOutOfBoundsException: Index -1 re
quested, with a size of 0

Here is my relevant code:

private static final String KEY_ROWID = "_id";
private static final String KEY_BRAKES = "brakes";
private static final String KEY_RPM="rpm";
private static final String DATABASE_NAME = "MyDB2";
private static final String DATABASE_TABLE = "performance";
private static final int DATABASE_VERSION=1;
private static final String DATABASE_CREATE = "create table performance (_id integer primary key autoincrement, " + "brakes text not null, rpm text not null);";


    //--insert an entry to the database--
public void insertData(String filter, String data){
    ContentValues entry = new ContentValues();

    if(filter.equals("b")){
        entry.put(KEY_BRAKES, data);
        Log.d(TAG,"inserted brake data");
        db.insert(DATABASE_TABLE, null, entry);
    }
    else if (filter.equals("r")){
        entry.put(KEY_RPM, data);
        Log.d(TAG,"inserted rpm data");
        db.insert(DATABASE_TABLE, null, entry);
    }


}

    //--retrive all entries
public Cursor getAllData(){
    Log.d(TAG,"about to query");
    try{
    Cursor c = db.query(DATABASE_TABLE, new String[]{KEY_BRAKES,KEY_RPM}, null, null, null, null, null);
    Log.d(TAG,"id=" + c.getString(0));

    return c;
    }catch(Exception e){
        Log.d (TAG,"error");
        Log.e(TAG,e.toString());
    }

    return null;
}

And the way that i enter items is as follows:

brakeRead = brakes.getText().toString();
rpmRead = brakes.getText().toString();
db.open();
db.insertData("b", brakeRead);
db.insertData("r", rpmRead);
db.close();

And the way i query is as follows:

                db.open();
            Cursor c = db.getAllData();
            if(c.moveToFirst())
            {
                do{
                    output.append("id: " +  c.getString(0) + " brakes: " + c.getString(1) + " rpm: " + c.getString(2));
                }while(c.moveToNext());
            }
            db.close();

But i am getting this error while querying: android.database.CursorIndexOutOfBoundsException: Index -1 re
quested, with a size of 0

Can anyone see if i am doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静 2024-12-08 15:25:24

查询非常适合检索数据,但出现如下错误,这意味着您的数据库中没有记录,所以我认为插入数据库时缺少一些内容

android.database.CursorIndexOutOfBoundsException:索引-1 请求,大小为 0

Query is perfect to retieve data but you got error as listed below it means you have no record in your database, so i think there is something missing while inserting database

android.database.CursorIndexOutOfBoundsException: Index -1 re quested, with a size of 0

ぺ禁宫浮华殁 2024-12-08 15:25:24

您已将刹车和转速设置为非空,但您一次将数据插入到每一列中。我建议按如下方式重写 insertData:

public void insertData(String brakes, String rpm){
ContentValues entry = new ContentValues();

    entry.put(KEY_BRAKES, brakes);
    Log.d(TAG,"inserted brake data");

    entry.put(KEY_RPM, rpm);
    Log.d(TAG,"inserted rpm data");
    db.insert(DATABASE_TABLE, null, entry);
}

然后您可以这样调用它:

brakeRead = brakes.getText().toString();
rpmRead = brakes.getText().toString();
db.open();
db.insertData(brakeRead, rpmRead);
db.close();

You have set both your brakes and rpm to not null, yet you are inserting data into each column one at a time. I would suggest rewriting insertData as follows:

public void insertData(String brakes, String rpm){
ContentValues entry = new ContentValues();

    entry.put(KEY_BRAKES, brakes);
    Log.d(TAG,"inserted brake data");

    entry.put(KEY_RPM, rpm);
    Log.d(TAG,"inserted rpm data");
    db.insert(DATABASE_TABLE, null, entry);
}

You would then call it as such:

brakeRead = brakes.getText().toString();
rpmRead = brakes.getText().toString();
db.open();
db.insertData(brakeRead, rpmRead);
db.close();
半夏半凉 2024-12-08 15:25:24

将您的代码从

if(c.moveToFirst())
            {
                do{
                    output.append("id: " +  c.getString(0) + " brakes: " + c.getString(1) + " rpm: " + c.getString(2));
                }while(c.moveToNext());
            }

更改为

            while(c.moveToNext()){
                output.append("id: " +  c.getString(0) + " brakes: " + c.getString(1) + " rpm: " + c.getString(2));
            }

更新

您的查询是

Cursor c = db.query(DATABASE_TABLE, new String[]{KEY_BRAKES,KEY_RPM}, null, null, null, null, null);

选择两列(BREAKS,RPM)。但是您正在查询3
将其更改为

Cursor c = db.query(DATABASE_TABLE, new String[]{KEY_ROWID,KEY_BRAKES,KEY_RPM}, null, null, null, null, null);

Change your Code from

if(c.moveToFirst())
            {
                do{
                    output.append("id: " +  c.getString(0) + " brakes: " + c.getString(1) + " rpm: " + c.getString(2));
                }while(c.moveToNext());
            }

to

            while(c.moveToNext()){
                output.append("id: " +  c.getString(0) + " brakes: " + c.getString(1) + " rpm: " + c.getString(2));
            }

Update

Your query is

Cursor c = db.query(DATABASE_TABLE, new String[]{KEY_BRAKES,KEY_RPM}, null, null, null, null, null);

which is selecting two columns(BREAKS,RPM) .But you are querying for 3
change it to

Cursor c = db.query(DATABASE_TABLE, new String[]{KEY_ROWID,KEY_BRAKES,KEY_RPM}, null, null, null, null, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文