我使用Firefox SQLite Manager创建并填充了一个SQLite数据库。我将.sqlite文件复制到Android项目的assets文件夹中。
我的目标是使用此记录来填充微调器。当我查询数据库时,光标显示列但没有记录。
我的DatabaseHandler
课程扩展了SQLiteOpenHelper
,其中包含onCreate
,onUpgrade
和getAllOffices
的方法。
public List<String> getAllOffices(){
List<String> offices = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT OfficeId as _id, OfficeName FROM " + TABLE_OFFICE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
offices.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning offices
return offices;
}
有关记录未归还的任何想法将不胜感激。
答案 0 :(得分:1)
试试这个:
public List<String> getAllOffices(){
List<String> offices = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT OfficeId as _id, OfficeName FROM " + TABLE_OFFICE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
offices.add(cursor.getString(cursor.getColumnIndex("_id")));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning offices
return offices;
}
基本上它只会确保您使用正确的列索引。否则,如果您正在使用eclipse,请尝试使用cellobject插件浏览数据库,并确保按预期填充表: http://marketplace.eclipse.org/content/cellobject-sqlite-xml-browser#.UWOU5RaBD3g
答案 1 :(得分:0)
请尝试这一点,可能对您有用。
public List<String> getAllOffices(){
List<String> offices = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_OFFICE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
System.out.println("CURSOR SIZE:"+cursor.getCount())
// looping through all rows and adding to list
if(cursor.getCount()!=0){
if (cursor.moveToFirst()) {
do {
System.out.println(cursor.getString(c.getColumnIndex("as per your db column name"));
} while (cursor.moveToNext());
}
}
// closing connection
cursor.close();
db.close();
// returning offices
return offices;
}
首先,如果你做得对,你必须看到控制台输出然后添加到你的office bean