这是我第一次使用sqlite
数据库。我正在尝试从retrieve
image
从数据库转到imageview
,但获取此error
,{{ 1}}。在从Couldn't read row 0, col -1 from CursorWindow
访问Cursor
之前,请确保data
已正确初始化。那是一天,仍然没有找到解决方案。
这是我的Activity
代码:
{
SQLiteDatabase db = null;
Cursor cursor=null;
byte[] img = null;
String tName = metName.getText().toString();
if(tName == null){
Intent i = new Intent(this,PhotoLogin.class);
startActivity(i);
}
try{
dh = new DataBaseHelperWithPhoto(getApplicationContext());
db = dh.getReadableDatabase();
cursor = db.rawQuery("Select * from " + dh.TABLE_NAME_2 ,null);
cursor.moveToFirst();
if(!cursor.isAfterLast()) {
do{
img = cursor.getBlob(cursor.getColumnIndex("PHOTO"));
}
while (cursor.moveToNext());
}
else{
}
Bitmap bmp = getImage(img);
mivPhoto.setImageBitmap(bmp);
}
catch (Exception ex){
ex.printStackTrace();
}
}
我的DataBaseHelper.Class
:
public class DataBaseHelperWithPhoto extends SQLiteOpenHelper implements BaseColumns {
public static final String DATABASE_NAME = "Employee.db";
public static final String TABLE_NAME = "PICTURE";
public static final String TABLE_NAME_2 = "USERPHOTO";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_1_NAME = "NAME";
public static final String PHOTO = "PH";
public static final String TABLE_2 = "create table "
+ TABLE_NAME_2 + "(" + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_1_NAME + " string, "
+ PHOTO + " string) ";
public DataBaseHelperWithPhoto(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
`db.execSQL(TABLE_2);`
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop table if EXISTS " + TABLE_NAME_2);
onCreate(db);
}
}
答案 0 :(得分:0)
您的列名称不匹配:
public static final String PHOTO = "PH";
和
img = cursor.getBlob(cursor.getColumnIndex("PHOTO"));
因此返回-1作为列索引。
请注意,Android sqlite CursorWindow每行的固有最大限制为2MB,因此您不应在列中存储任何“大数据”。
答案 1 :(得分:0)
在这一行img = cursor.getBlob(cursor.getColumnIndex("PHOTO"));
中,您要获取名为PHOTO的列,而不是名为PH的列。
如果无法找到列,则getColumnIndex
方法返回-1,并且永远不会有偏移量为-1的列。
您可以将上述行更改为
img = cursor.getBlob(cursor.getColumnIndex("PH")); //<<<< not the best way
或者更准确地将其更改为
img = cursor.getBlob(cursor.getColumnIndex(PHOTO));
此结果将列名解析为 PH