I was following this tutroial about using your own SQLite database. I did these steps as described in the tutorial:
- Preparing my database
- Copying the same EXACT
DataBaseHelperclass to my project package. You can see the class code there Then, I just added one method to the
DataBaseHelperclass which isfetchData. It is simply fetching a whole table with the given name:public Cursor fetchData(String table) { String[] selectionArgs = new String[] {"*"}; return myDataBase.query(table, null, null, selectionArgs, null, null, null); }After that, in one of my activity classes I did this:
DataBaseHelper myDbHelper; myDbHelper = new DataBaseHelper(this); try { myDbHelper.createDataBase(); } catch (IOException ioe) { throw new Error("Unable to create database"); } try { myDbHelper.openDataBase(); } catch (SQLException sqle) { throw sqle; } TextView txt = (TextView) findViewById(R.id.txt); try { //I will use my method to fetch a table named: myTable Cursor c = myDbHelper.fetchData("myTable"); if((Object)c.getCount() != null) txt.setText(c.getCount()); else txt.setText("null"); } catch(Exception e) { txt.setText("error"); }
However, I keep getting 'error' in the TextView. Is there a problem in my way?