* *这是我的数据库代码,当我使用getVenue()时,我得到异常.. 任何可以帮我解决这个问题???请
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + ID
+ " INTEGER PRIMARY KEY," + NAME + " TEXT," + BOOKMARK
+ " TEXT" + ")";
db.execSQL(CREATE_TABLE);
}
public void addVenue(Venue v) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID, v.getId());
values.put(NAME, v.getComment());
values.put(BOOKMARK, v.getBookmark());
db.insert(TABLE_NAME, null, values);
db.close();
}
public Venue getVenue(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME,
new String[] { ID, NAME, BOOKMARK }, ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Venue v = new Venue(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return v;
}
}
答案 0 :(得分:1)
在表声明中,ID被指定为整数,但是您尝试将其作为字符串查询(String.valueOf(id).SQLite中的字符串需要被“'”字符包围,例如'9 '。作为测试,尝试将搜索查询设置为“ID = 1”,并将whereargs为null,如下所示:
public Venue getVenue(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME,
new String[] { ID, NAME, BOOKMARK }, ID + "=1",
null, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Venue v = new Venue(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return v;
}
然后使用以下内容查看是否有效:
Log.d("cusor results for row 1: ", "Cursor Row: "+String.valueOf(cursor.getInt(0) + ", Name: " + cursor.getString(1) + ", Bookmark: " + cursor.getString(2))
另外,你的cursor.getString(0)是不正确的,因为你的第一列是你的ID列,它是一个整数,所以你会使用“cursor.getInt(0)”