-1

I am a student and first time studying android and trying to make a quiz app using SQLite it works properly on emulator but crashes on android mobile

I am not able to understand the problem whenever I am trying to install the quiz by copying its .apk it gets installed on android phone but whenever I click on the button to show questions it crashes

This is my db helper class.

package com.example.quiz;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    import android.widget.Toast;

    public class dbhelper extends SQLiteOpenHelper {
        private final static String  DATABASE_NAME="questio.db";
        private final static int  DATABASE_VERSION=2;
        private final static String  Android="quiz1";

        private final static String  php="quiz2";

        private final static String  ID="id";
        private final static String  score="score";


        private final static String  Question="question";
        private final static String  answer="ans";
        private final static String  op1="opa";
        private final static String  op2="opb";
        private final static String  op3="opc";

        public static final String SQL_Create="create table " +Android+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+Question+" TEXT NOT NULL, "+answer+" TEXT,"+op1+" TEXT, "+op2+" TEXT, "+op3+" TEXT"+")";
        public static final String SQL_Create2="create table " +php+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+Question+" TEXT NOT NULL, "+answer+" TEXT,"+op1+" TEXT, "+op2+" TEXT, "+op3+" TEXT"+")";







        SQLiteDatabase db1;
        public dbhelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(SQL_Create);
            db.execSQL(SQL_Create2);

            db1=db;
            db.close();


            // TODO Auto-generated method stub

        }

All statements of onUpgrade method are commented so that the questions inserted in the table is not lost.

@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {





        //db.execSQL("drop table if exists"+Android);

            //db.execSQL("drop table "+php);


            //db.execSQL("drop table if exist"+TableName);
            //onCreate(db);

            // TODO Auto-generated method stubS

        }
        public void insertIntoTable(String str1,String str2,String str3,String str5,String str4)
        {
             db1=getWritableDatabase();

            ContentValues values=new ContentValues();
            values.put(Question, str1);
            values.put(answer, str2);
            values.put(op1, str3);
            values.put(op2, str5);
            values.put(op3, str4);
            db1.insert(Android, null, values);

        }
        public void insertIntoTable2(String str1,String str2,String str3,String str4,String str5)
        {
            SQLiteDatabase db=getWritableDatabase();

            ContentValues values=new ContentValues();
            values.put(Question, str1);
            values.put(answer, str2);
            values.put(op1, str3);
            values.put(op2, str5);
            values.put(op3, str4);
            db1.insert(php, null, values);

        }




        public List<Question> getAllQuestions() {
            List<Question> quesList = new ArrayList<Question>();
            // Select All Query
            String selectQuery = "SELECT * FROM " +Android;
        //String del="delete from "+Android+ " where ID=16";
            SQLiteDatabase db2=this.getReadableDatabase();
            //db2.execSQL(del);
        Log.d("chk", selectQuery);
            Cursor cursor = db2.rawQuery(selectQuery, null);
            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Question quest = new Question();
                    quest.setID(cursor.getInt(0));
                    quest.setQUESTION(cursor.getString(1));
                    Log.d("quest", quest.getQUESTION());
                    quest.setANSWER(cursor.getString(2));
                    quest.setOPTA(cursor.getString(3));
                    quest.setOPTB(cursor.getString(5));
                    quest.setOPTC(cursor.getString(4));
                    quesList.add(quest);
                } while (cursor.moveToNext());
            }
            // return quest list
            return quesList;
        }

        public int rowcount()
        {
            int row=0;
            String selectQuery = "SELECT  * FROM " + Android;
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            row=cursor.getCount();
            return row;
        }


    }
ORIGINAL
  • 179
  • 1
  • 12
sakshi
  • 139
  • 1
  • 1
  • 6

1 Answers1

1

For any crash, see the logcat for helpful information to locate the problem. Always include the stacktrace in your questions, too. See Unfortunately MyApp has stopped. How can I solve this? for more.

In the code, don't call close() on the SQLiteDatabase that was passed to your helper's onCreate().

Why it "works" on emulator is possible because if the database file already exists (set up with an earlier version of your code), the onCreate() callback is not invoked. It's only called when the database file was just created. You'll see a crash if you e.g. uninstall your app on the emulator or clear its data. See When is SQLiteOpenHelper onCreate() / onUpgrade() run? for more.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303