public HashMap<String,String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
System.out.println("select query:"+selectQuery);
System.out.println("entering into hashmap");
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
System.out.println("cursor count is");
System.out.println(cursor.getCount());
if(cursor.getCount() > 0){
System.out.println("enter into if of hashmap");
user.put("username", cursor.getString(1));
user.put("name", cursor.getString(2));
user.put("email", cursor.getString(3));
user.put("user_id", cursor.getString(4));
user.put("country", cursor.getString(5));
user.put("state", cursor.getString(6));
user.put("city", cursor.getString(7));
user.put("registered_on", cursor.getString(8));
}
cursor.close();
db.close();
// return user
return user;
}
错误:无法从CursorWindow读取第0行,第8列。确保Cursor在从其访问数据之前已初始化。
无法从SQLiteDatabase中获取任何数据
感谢
答案 0 :(得分:0)
在Cursor中,列索引基于0。
如果你想从第一列获取数据,那么你应该使用cursor.getString(0)
而不是cursor.getString(1)
所以你需要更换
if(cursor.getCount() > 0){
System.out.println("enter into if of hashmap");
user.put("username", cursor.getString(1));
user.put("name", cursor.getString(2));
user.put("email", cursor.getString(3));
user.put("user_id", cursor.getString(4));
user.put("country", cursor.getString(5));
user.put("state", cursor.getString(6));
user.put("city", cursor.getString(7));
user.put("registered_on", cursor.getString(8));
}
通过以下代码......
if(cursor.getCount() > 0){
System.out.println("enter into if of hashmap");
user.put("username", cursor.getString(0));
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("user_id", cursor.getString(3));
user.put("country", cursor.getString(4));
user.put("state", cursor.getString(5));
user.put("city", cursor.getString(6));
user.put("registered_on", cursor.getString(7));
}
在您的代码中,它试图从第9列获取不存在的数据。
答案 1 :(得分:0)
尝试使用此代码:使用cursor.getString(0)代替cursor.getString(1)
public HashMap<String,String> getUserDetails(){
ArrayList<HashMap<String, String>> wordList;
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
System.out.println("select query:"+selectQuery);
System.out.println("entering into hashmap");
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do
{
HashMap<String,String> user = new HashMap<String,String>();
user.put("username", cursor.getString(0));
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("user_id", cursor.getString(3));
user.put("country", cursor.getString(4));
user.put("state", cursor.getString(5));
user.put("city", cursor.getString(6));
user.put("registered_on", cursor.getString(7));
wordList.add(user);
}while(cursor.moveToNext);
return wordList
}
else
{
System.out.println("No data found");
return null;
}
}
我们可以使用此代码显示数据。
ArrayList<HashMap<String, String>> result;
result= Objectname.getdetails();
for(i=0;i<result.size();i++)
{
HashMap<String,String> content = new HashMap<String, String>();
content = result.get(i);
String username=content.get("username").toString();
String name=content.get("name").toString();
String email=content.get("email").toString();
String uid=content.get("user_id").toString();
String country=content.get("country").toString();
String state=content.get("state").toString();
String city=content.get("city").toString();
String reg=content.get("registered_on").toString();
}
答案 2 :(得分:0)
更改从1开始的光标索引。从0开始到7,它将如下所示:
if(cursor.getCount() > 0){
user.put("username", cursor.getString(0));
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("user_id", cursor.getString(3));
user.put("country", cursor.getString(4));
user.put("state", cursor.getString(5));
user.put("city", cursor.getString(6));
user.put("registered_on", cursor.getString(7));
}