0

我已经编写了将图像存储在 sqlitedatabase 中的代码,还编写了获取所有记录的方法,但是在获取数据时,我得到了异常为 Nullpointer 异常。我在 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;

    }


} 
4

1 回答 1

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;
    }
于 2012-10-12T06:29:18.010 回答