This is code for Employee class.
public class Employee {
String name;
String email;
byte[] picture;
public Employee(String name, String email, byte[] picture) {
this.name = name;
this.email = email;
this.picture = picture;
}
}
And this is my code to get data from Database which is currently not working because i could'nt figure out how to get these three things (String name, email and byte array image) from database. However i have successfully inserted all three things into Sqlite database.
**Edit : ** This is my database class complete code.
package com.cplusplusapp.rashidfaheem.hybridsoftwaresolutions.hbss.rashidfaheem.image2database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class Sqlite_database extends SQLiteOpenHelper {
private static final String DATABASE_NAME="myCustomerDatabase";
private static final String DATABASE_TABLE="Customers";
private static final int DATABASE_VERSION=1;
public static final String KEY_ROWID="id";
public static final String KEY_Name="customer_name";
public static final String KEY_Email="customer_email";
public static final String KEY_IMAGE="image";
private SQLiteDatabase ourDatabase;
ArrayList<Employee> elist;
public Sqlite_database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_Name + " TEXT, " +
KEY_Email + " TEXT, " +
KEY_IMAGE + " BLOB);"
);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(sqLiteDatabase);
}
public List<Employee> getImage(){
List<Employee> employee_list = new ArrayList<>();
ourDatabase = this.getReadableDatabase();
String[] field = {KEY_Name,KEY_Email,KEY_IMAGE};
Cursor c = ourDatabase.query(DATABASE_TABLE, field, null, null, null, null, null);
int iname = c.getColumnIndex(KEY_Name);
int iemail = c.getColumnIndex(KEY_Email);
int image = c.getColumnIndex(KEY_IMAGE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
String name = c.getString(iname);
String email = c.getString(iemail);
byte[] pic = c.getBlob(image);
employee_list.add(new Employee(name, email, pic));
}
return employee_list;
}
public long createImageEntry(String name, String email, byte[] image) {
ourDatabase=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(KEY_Name, name);
cv.put(KEY_Email, email);
cv.put(KEY_IMAGE, image);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
}
And this is code on button click to get name and email from that return arraylist and show it through textviews. But getting error. java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sql = new Sqlite_database(MainActivity.this);
List<Employee> employees = sql.getImage();
for (Employee e : employees) {
r_name = r_name + e.name;
r_email = r_email + e.email;
}
tvName.setText(r_name);
tvEmail.setText(r_email);
}
});