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;
}
error:couldn't read row 0, col 8 from CursorWindow. make sure the Cursor is initialized correclty before accessing data from it.
not able to fetch any data from SQLiteDatabase
thanks
In a Cursor, column index is 0 based.
That is if you want to fetch data from 1st column then you should use cursor.getString(0)
instead of cursor.getString(1)
So you need to replace
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));
}
by following code....
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));
}
In your code its trying to fetch data from 9th column which does not exists.
Try to use this code: use cursor.getString(0) instead of 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;
}
}
we can display data using this code.
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();
}
Change your index of cursor which is starting from 1.. start from 0 to 7. and it will work like below:
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));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.