JDS*_*imz 4 java sqlite android 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;
}
Run Code Online (Sandbox Code Playgroud)
这是调用它的代码:
String incomingMessage = " ";
incomingMessage = in.readLine() + System.getProperty("line.separator");
devName = devDB.getDevName(incomingMessage);
Run Code Online (Sandbox Code Playgroud)
我知道这是获取信息,因为它记录:
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)
Run Code Online (Sandbox Code Playgroud)
我试过检查非空游标.所有这一切都告诉我什么.我知道有数据我要求它看起来我只是不知道为什么它不会显示它.谁能告诉我哪里出错了?谢谢!
bwe*_*egs 15
你在用SQLiteOpenHelper吗?如果是这样,您需要在尝试读取或写入db数据库之前将数据库变量设置为数据库,如下所示:
db = this.getWritableDatabase();
Run Code Online (Sandbox Code Playgroud)
...你的新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;
}
Run Code Online (Sandbox Code Playgroud)
public SQLiteDatabase getWritableDatabase() - 第一次调用它时,将打开数据库并调用onCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase,int,int)和/或onOpen(SQLiteDatabase).成功打开后,数据库将被缓存,因此您可以在每次需要写入数据库时调用此方法.(确保在不再需要数据库时调用close().)错误权限或完整磁盘等错误可能导致此方法失败,但如果问题得到解决,将来的尝试可能会成功.
扩展@bwegs关于呼叫的答案getWritableDatabase().
尝试使用selectionArgs它,因为它更干净.检查返回值moveToFirst(),并确保关闭光标以避免内存泄漏.
public String getDevName(String devid) {
db = myDBHelper.getWritableDatabase(); //get db if not done so already, you may have already done this in code not shown
String name = null;
String where = KEY_DEVID + " like ?";
String[] selectionArgs = new String[] {"%"+ devid + "%" };
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, selectionArgs , null, null, null, null);
//Only use cursor if moveToFirst() succeeds
//this will make it so that you don't get the CursorIndexOutOfBoundsException
if (c.moveToFirst()){
name = c.getString(COL_DEVICE);
}
c.close(); //close your cursor to avoid memory leak!
db.close(); //close here if you call getWritableDatabase() in beginning of this function
//This will return null if no results from query
return name;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1074 次 |
| 最近记录: |