1. Are you ready for the Galaxy S20? Here is everything we know so far!

Problems with SQLite DB

Discussion in 'Android Development' started by gilbert111, Oct 21, 2014.

  1. gilbert111

    gilbert111 Lurker
    Thread Starter

    Hello Community,
    I have a small quiz app project, that fetches the question and answer strings from a sqlite database. Most of the code I took from a book and changed it for my use, since I have no experience with this.
    In the following code, the app always crashes when I open the Question activity, so when onCreate() gets called.
    This is the activity code:
    Code (Text):
    1.  
    2. public class Question extends ActionBarActivity {
    3.    
    4.     questionsDBHelper db;
    5.     int counter = 0;
    6.     String module;
    7.     String[][] questions = new String[questionsDBHelper.NUMBER_OF_QUESTIONS][6];
    8.     String[] answers = new String[4];
    9.     int corr;
    10.     TextView questionText;
    11.     Button answ1;
    12.     Button answ2;
    13.     Button answ3;
    14.     Button answ4;
    15.     int points = 0;
    16.     int correctAnswer;
    17.    
    18.     @Override
    19.     protected void onCreate(Bundle savedInstanceState) {
    20.         questionText = (TextView)findViewById(R.id.questionText);
    21.         answ1 = (Button)findViewById(R.id.answ1);
    22.         answ2 = (Button)findViewById(R.id.answ2);
    23.         answ3 = (Button)findViewById(R.id.answ3);
    24.         answ4 = (Button)findViewById(R.id.answ4);
    25.         super.onCreate(savedInstanceState);
    26.         setContentView(R.layout.activity_question);
    27.          String destDir = "/data/data/" + getPackageName() + "/databases/";
    28.             String destPath = destDir + "quizDB";
    29.             File f = new File(destPath);
    30.             if (!f.exists()) {
    31.                 //---make sure directory exists---
    32.                 File directory = new File(destDir);
    33.                 directory.mkdirs();
    34.                 //---copy the db from the assets folder into
    35.                 // the databases folder---
    36.                 try {
    37.                     CopyDB(getBaseContext().getAssets().open("quizDB"),
    38.                             new FileOutputStream(destPath));
    39.                 } catch (FileNotFoundException e) {    
    40.                     e.printStackTrace();
    41.                 } catch (IOException e) {
    42.                     e.printStackTrace();
    43.                 }
    44.                 db = new questionsDBHelper(this);
    45.                 Intent receivedIntent = getIntent();
    46.                 Bundle b = getIntent().getExtras();
    47.                 module = b.getString("moduleCode");
    48.                 questions = db.createTable(module);
    49.                
    50.             }
    51.             else{
    52.                 Toast toast = Toast.makeText(getApplicationContext(), "No Database found!", Toast.LENGTH_SHORT);
    53.                 toast.show();
    54.             }
    55.            
    56.            
    57.            
    58.             //First Question
    59.            
    60.             questionText.setText(questions[0][0]);
    61.            
    62.             answ1.setText(questions[0][1]);
    63.             answ2.setText(questions[0][2]);
    64.             answ3.setText(questions[0][3]);
    65.             answ4.setText(questions[0][4]);
    66.             correctAnswer = Integer.parseInt(questions[0][5]);
    67.     }
    68.    
    69.     public void CopyDB(InputStream inputStream, OutputStream outputStream)
    70.             throws IOException {
    71.                 //---copy 1K bytes at a time---
    72.                 byte[] buffer = new byte[1024];
    73.                 int length;
    74.                 while ((length = inputStream.read(buffer)) > 0) {
    75.                     outputStream.write(buffer, 0, length);
    76.                 }
    77.                 inputStream.close();
    78.                 outputStream.close();
    79.             }
    80.  
    This is the DBHelper:
    Code (Text):
    1.  
    2. public class questionsDBHelper {
    3.     static final String TAG = "questionsDBHelper";
    4.     public static final String TABLE_NAME = "QUESTIONS";
    5.     public static final String COLUMN_NAME_ID = "ID";
    6.     public static final String COLUMN_NAME_MODULE = "MODULE";
    7.     public static final String COLUMN_NAME_QUESTION = "QUESTION";
    8.     public static final String COLUMN_NAME_ANSWER_1 = "ANSWER_1";
    9.     public static final String COLUMN_NAME_ANSWER_2 = "ANSWER_2";
    10.     public static final String COLUMN_NAME_ANSWER_3 = "ANSWER_3";
    11.     public static final String COLUMN_NAME_ANSWER_4 = "ANSWER_4";
    12.     public static final String COLUMN_NAME_CORRECT = "CORRECT";
    13.     private static final String SQL_CREATE = "CREATE    TABLE" + TABLE_NAME + "("
    14.             + COLUMN_NAME_ID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
    15.             + COLUMN_NAME_MODULE + "TEXT NOT NULL," + COLUMN_NAME_QUESTION
    16.             + "TEXT NOT NULL," + COLUMN_NAME_ANSWER_1 + COLUMN_NAME_ANSWER_1
    17.             + "TEXT NOT NULL," + COLUMN_NAME_ANSWER_2 + "TEXT NOT NULL,"
    18.             + COLUMN_NAME_ANSWER_3 + "TEXT NOT NULL," + COLUMN_NAME_ANSWER_4
    19.             + "TEXT NOT NULL," + COLUMN_NAME_CORRECT + "TEXT NOT NULL);";
    20.  
    21.     private static final String SQL_DELETE = "DROP TABLE IF EXISTS "
    22.             + TABLE_NAME;
    23.     public static final int DATABASE_VERSION = 1;
    24.     public static final String DATABASE_NAME = "quizDB";
    25.     public static final int NUMBER_OF_QUESTIONS = 3;
    26.     SQLiteDatabase db;
    27.     DatabaseHelper DBHelper;
    28.     final Context context;
    29.    
    30.     public questionsDBHelper(Context ctx) {
    31.         this.context = ctx;
    32.         DBHelper = new DatabaseHelper(context);
    33.     }
    34.  
    35.     private static class DatabaseHelper extends SQLiteOpenHelper
    36.     {
    37.         DatabaseHelper(Context context)
    38.         {
    39.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
    40.         }
    41.  
    42.         @Override
    43.         public void onCreate(SQLiteDatabase db)
    44.         {
    45.             try {
    46.                 db.execSQL(SQL_CREATE);
    47.             } catch (SQLException e) {
    48.                 e.printStackTrace();
    49.             }
    50.         }
    51.  
    52.         @Override
    53.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    54.         {
    55.             Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
    56.                     + newVersion + ", which will destroy all old data");
    57.             db.execSQL("DROP TABLE IF EXISTS QUESTIONS");
    58.             onCreate(db);
    59.         }
    60.     }
    61.    
    62.     public questionsDBHelper open() throws SQLException{
    63.         db = DBHelper.getWritableDatabase();
    64.         return this;
    65.     }
    66.    
    67.      public void close()
    68.         {
    69.             DBHelper.close();
    70.         }
    71.  
    72.     public Cursor getRandomQuestion(String module) {
    73.         int i;
    74.         Random r = new Random();
    75.         Cursor mCursor = db.query(true, TABLE_NAME, new String[] {
    76.                 COLUMN_NAME_ID, COLUMN_NAME_MODULE, COLUMN_NAME_ANSWER_1,
    77.                 COLUMN_NAME_ANSWER_2, COLUMN_NAME_ANSWER_3,
    78.                 COLUMN_NAME_ANSWER_4, COLUMN_NAME_CORRECT }, COLUMN_NAME_MODULE
    79.                 + "=" + module, null, null, null, null, null);
    80.         if (mCursor != null) {
    81.             i = mCursor.getCount();
    82.             mCursor.moveToFirst();
    83.             mCursor.move(r.nextInt(i-1));
    84.         }
    85.         return mCursor;
    86.     }
    87.    
    88.     public String[][] createTable(String module){
    89.         String[][] roundQuestions = new String[NUMBER_OF_QUESTIONS][6];
    90.         for(int i = 0; i < NUMBER_OF_QUESTIONS; i++){
    91.             roundQuestions[i][0] = getRandomQuestion(module).getString(2);
    92.             roundQuestions[i][1] = getRandomQuestion(module).getString(3);
    93.             roundQuestions[i][2] = getRandomQuestion(module).getString(4);
    94.             roundQuestions[i][3] = getRandomQuestion(module).getString(5);
    95.             roundQuestions[i][4] = getRandomQuestion(module).getString(6);
    96.             roundQuestions[i][5] = Integer.toString(getRandomQuestion(module).getInt(7));
    97.         }
    98.         return roundQuestions;
    99.     }
    100.    
    101. }
    102.  
    I appreciate your help!
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...