Android SQLite查询问题
我正在学习如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查询非常适合检索数据,但出现如下错误,这意味着您的数据库中没有记录,所以我认为插入数据库时缺少一些内容
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
您已将刹车和转速设置为非空,但您一次将数据插入到每一列中。我建议按如下方式重写 insertData:
然后您可以这样调用它:
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:
You would then call it as such:
将您的代码从
更改为
更新
您的查询是
选择两列(BREAKS,RPM)。但是您正在查询3
将其更改为
Change your Code from
to
Update
Your query is
which is selecting two columns(BREAKS,RPM) .But you are querying for 3
change it to