在Oncreate上运行sqlite会得到没有跟踪的错误

时间:2014-06-18 03:02:06

标签: android sqlite

我一直在为我的项目中发生的错误而烦恼,我无法找到或追踪错误......我只是想澄清是否可以在oncreate中执行sqlite语句或者在oncreate中运行方法以下面的代码为例:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        datafromsqlite();
    }

public void datafrom sqlite(){
        Cursor list = DBNAME.rawQuery("SELECT * FROM LIST_TBL");
        list.moveToFirst();
        if(list.getCount > 0){
             Log.e(list.getString(1),list.getString(2));
        }else{
             list.close();
    }
}

或者像这样

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list);

            Cursor list = DBNAME.rawQuery("SELECT * FROM LIST_TBL");
            list.moveToFirst();
            if(list.getCount > 0){
                 Log.e(list.getString(1),list.getString(2));
            }else{
                 list.close();
        }
        }

更新:

@Override
     protected void onPause() {
      super.onPause();

      if (DBNAME.isOpen()) {
          DBNAME.close();
      }
     }
    @Override
     protected void onResume() {
      super.onResume();
      DBNAME = openOrCreateDatabase(DATABASE_NAME,
                SQLiteDatabase.CREATE_IF_NECESSARY 
                  | SQLiteDatabase.OPEN_READWRITE, null);

             }
}

1 个答案:

答案 0 :(得分:0)

你没有得到数据库对象,你需要创建一个扩展SQLiteOpenHelper类的类,该类包含方法getReadableDatabase()

SQLiteDatabase db;
DbHelper dbh=new DbHelper(context);
db=dbh.getReadableDatabase();

DbHelper是我创建的一个扩展SQLiteOpenHelper

的类