异常而从SqliteDatabase Retriving数据 [英] Exception while Retriving data from the SqliteDatabase

查看:183
本文介绍了异常而从SqliteDatabase Retriving数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了code来存储sqlitedatabase形象,也将写入GETALL记录的方法,但同时获得的数据我得到异常的空指针Exception.I我做错了什么在全选方法是什么?

 公共类DBUserAdapter {    私有静态最后弦乐DATABASE_NAME =用户;
    私有静态最终诠释DATABASE_VERSION = 1;    私有静态最后弦乐的UserDetails =
            创建表的UserDetails(usersno整数主键自动增量,照片BLOB,日期文字不是null);;    私人上下文的背景下= NULL;
    私人DatabaseHelper DBHelper;
    私人SQLiteDatabase分贝;
    公共DBUserAdapter(上下文的背景下){
        this.context =背景;
        DBHelper =新DatabaseHelper(背景);    }
    私有静态类DatabaseHelper扩展SQLiteOpenHelper
    {
        DatabaseHelper(上下文的背景下)
        {
            超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        }        公共无效的onCreate(SQLiteDatabase DB){
            db.execSQL(的UserDetails);        }        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
            // TODO自动生成方法存根
            db.execSQL(DROP TABLE IF EXISTS用户);
            的onCreate(DB);
        }
    }
    公共无效的open()抛出的SQLException
    {
        DB = DBHelper.getWritableDatabase();
        返回;
    }    公共无效的close()
    {
        DBHelper.close();
    }
    众长刀片(字节[]的照片,日期字符串)
    {
        ContentValues​​ initialValues​​ =新ContentValues​​();
        initialValues​​.put(照片,照片);
        initialValues​​.put(日,日期);
        // initialValues​​.put(KEY_TIME,时间);
        Log.d(inotvalues​​sssssssss,initialValues​​.toString());
        Log.d(dbbbbbbbbbb ++++++ *******,db.toString());
        返回db.insert(的UserDetails,空,initialValues​​);    }    公共光标全选(字符串TABLE_NAME,字符串列,串选择,字符串[] SELECTION_ARGS,
            字符串GROUP_BY,HAVING字符串,字符串OREDER_BY){
        // TODO自动生成方法存根
        光标光标= this.db.query(TABLE_NAME,新的String [] {}柱,
                选择,SELECTION_ARGS,GROUP_BY,HAVING OREDER_BY);
        返回游标;    }
}


解决方案

创建一个ArrayList,将举行从数据库中收集的所有数据。

 的ArrayList< ArrayList的<对象>> dataArrays =新的ArrayList< ArrayList的<对象>>();

这是创建光标对象的数据库调用。
游标对象存储在数据库中收集的信息,并用于通过数据进行迭代。

复合剂code在这里...

 公开的ArrayList< ArrayList的<对象>> getAllRowsAsArrays()
    {        ArrayList的< ArrayList的<对象>> dataArrays =新的ArrayList< ArrayList的<对象>>();
        光标光标;        尝试
        {
            //查询数据库对象创建光标。
            光标= db.query(
                    TABLE_NAME,
                    新的String [] {TABLE_ROW_ID,TABLE_ROW_ONE,TABLE_ROW_TWO},
                    NULL,NULL,NULL,NULL,NULL
            );            //移动光标指针定位为零。
            cursor.moveToFirst();            //如果在当前光标位置之后的数据,将其添加
            //到ArrayList。
            如果(!cursor.isAfterLast())
            {
                做
                {
                    ArrayList的<对象> DataList控件=新的ArrayList<对象>();                    dataList.add(cursor.getLong(0));
                    dataList.add(cursor.getString(1));
                    dataList.add(cursor.getString(2));                    dataArrays.add(DataList控件);
                }
                //移动光标的指针向上移动一个位置。
                而(cursor.moveToNext());
            }
        }
        赶上(的SQLException E)
        {
            Log.e(DB错误,e.toString());
            e.printStackTrace();
        }        //返回拥有收集的数据ArrayList中
        //数据库。
        返回dataArrays;
    }

I have written the code to Store the image in sqlitedatabase and also written the method to getall the records,but while getting data I am getting Exception as Nullpointer Exception.I am doing anything wrong in selectall method ?

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;

    }


} 

解决方案

create an ArrayList that will hold all the data collected from the database.

ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();

This is a database call that creates a "cursor" object. The cursor object store the information collected from the database and is used to iterate through the data.

Complet Code here...

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;
    }

这篇关于异常而从SqliteDatabase Retriving数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆