我编写了代码来将图像存储在sqlitedatabase中,并编写了获取记录的方法,但是在获取数据的过程中,我得到了Exception为Nullpointer Exception。我在selectall方法中做错了吗?
public class DBUserAdapter {
private static final String DATABASE_NAME = "users";
private static final int DATABASE_VERSION = 1;
private static final String USERDETAILS=
"create table userdetails(usersno integer primary key autoincrement,photo BLOB,date text not null);";
private Context context = null;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBUserAdapter(Context context) {
this.context = context;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(USERDETAILS);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
public void open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return;
}
public void close()
{
DBHelper.close();
}
public long insert(byte[] photo, String date)
{
ContentValues initialValues = new ContentValues();
initialValues.put("photo", photo);
initialValues.put("date", date);
// initialValues.put(KEY_TIME, time);
Log.d("inotvaluessssssssss",initialValues.toString());
Log.d("dbbbbbbbbbb++++++*******", db.toString());
return db.insert("userdetails", null, initialValues);
}
public Cursor selectAll(String TABLE_NAME, String COLUMNS, String SELECTION, String[] SELECTION_ARGS,
String GROUP_BY, String HAVING, String OREDER_BY) {
// TODO Auto-generated method stub
Cursor cursor = this.db.query(TABLE_NAME, new String[] { COLUMNS },
SELECTION, SELECTION_ARGS, GROUP_BY, HAVING, OREDER_BY);
return cursor;
}
}
答案 0 :(得分:0)
创建一个ArrayList,它将保存从数据库中收集的所有数据。
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
这是一个创建“游标”对象的数据库调用。 游标对象存储从数据库收集的信息,用于迭代数据。
在此完成代码......
public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
{
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
Cursor cursor;
try
{
// ask the database object to create the cursor.
cursor = db.query(
TABLE_NAME,
new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
null, null, null, null, null
);
// move the cursors pointer to position zero.
cursor.moveToFirst();
// if there is data after the current cursor position, add it
// to the ArrayList.
if (!cursor.isAfterLast())
{
do
{
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataArrays.add(dataList);
}
// move the cursor's pointer up one position.
while (cursor.moveToNext());
}
}
catch (SQLException e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
// return the ArrayList that holds the data collected from
// the database.
return dataArrays;
}