[英]Cannot Fetch a Data From Database
First of all i'm new to Android Development. 首先,我是Android开发的新手。 I'm having some issues with fetching data from database.
我从数据库中获取数据时遇到一些问题。 Whenever i try to fetch a data, the cursor is being empty.
每当我尝试获取数据时,游标就为空。
Here is my code: 这是我的代码:
public ArrayList<Word> getWords(){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
ArrayList<Word> List = new ArrayList<Word>();
if(cursor != null){
cursor.moveToFirst();
while (cursor.moveToNext()){
List.add(new Word(cursor.getString(cursor.getPosition())));
}
}
cursor.close();
db.close();
return List;
}
The size of the cursor is always 1 but the size of the "List" variable is always 0. 游标的大小始终为1,但“列表”变量的大小始终为0。
I didn't understand the problem, thanks for helping me. 我不明白问题所在,感谢您的帮助。
You are shifting the cursor position twice. 您两次移动光标位置。 Just remove the line cursor.moveToFirst();
只需删除cursor.moveToFirst();行即可。 The moveToNext method is sufficient as it moves to the first position on the first iteration.
moveToNext方法已足够,因为它在第一次迭代中移到了第一位置。
you can use this code: 您可以使用以下代码:
1-If you need first row: 1-如果您需要第一行:
if(cursor != null){
cursor.moveToFirst();
if(cursor.isAfterLast() == false){
List.add(new Word(cursor.getString(cursor.getPosition())));
}
}
2- if you need several rows : 2-如果您需要几行:
if(cursor != null){
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
List.add(new Word(cursor.getString(cursor.getPosition())));
cursor.moveToNext();
}
}
I checked everything but your recommendations didn't worked. 我检查了所有内容,但您的建议没有用。 Just to be sure: I want to fetch all rows.
只是为了确保:我想获取所有行。
Try like this 这样尝试
public ArrayList<Word> getWords(){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
ArrayList<Word> list = new ArrayList<Word>();
if(cursor != null){
while (cursor.moveToNext()){
list.add(new Word(cursor.getString(cursor.getPosition())));
}
}
cursor.close();
db.close();
return list;
}
I just realised that the program does not go into the while loop in the code that i given above. 我只是意识到该程序不会进入我上面给出的代码的while循环中。 I think there is a problem with database.
我认为数据库存在问题。
I used that query to create database: 我使用该查询创建数据库:
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_NAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME_TURKISH + " TEXT, " +
COLUMN_NAME_ENGLISH + " TEXT, " +
COLUMN_NAME_GERMAN + " TEXT, " +
COLUMN_NAME_FRENCH + " TEXT, " +
COLUMN_NAME_ARABIC + " TEXT, " +
COLUMN_NAME_RUSSIAN + " TEXT, " +
COLUMN_NAME_SPANISH + " TEXT, " +
COLUMN_NAME_ITALIAN + " TEXT)";
I feel like a idiot because i forgot to type the database name at the constructor method like that: 我感觉像个白痴,因为我忘了像这样在构造函数方法中键入数据库名称:
Old 旧
super(context, null, null, 1);
Now 现在
super(context, "database", null, 1);
Thanks for your help! 谢谢你的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.