Naveen Chauhan
Naveen Chauhan

Reputation: 2026

I want to create a database using SQLiteOpenHelper but i am facing some problems

I have created a sub class (DatabaseHelper) of SQLiteOpenHelper Class.Its has a constructor and two overriden methods

 public DatabaseHelper(Context context) {
            super(context, dbName, null,33);
            // TODO Auto-generated constructor stub
        }
public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE "+ quizTable +"("+quizCol1+" INTEGER PRIMARY KEY,"+
                quizCol2+" TEXT,"+ quizCol3 +" TEXT,"+ quizCol4 +" INTEGER," + quizCol5 + " DATETIME )");

        db.execSQL("CREATE TABLE "+ questionTable +"("+questionCol1+" INTEGER PRIMARY KEY,"+
                questionCol2+" INTEGER,"+ questionCol3 +" TEXT,"+ questionCol4 +" TEXT," + questionCol5 + " TEXT," +
                questionCol6 + " TEXT," + questionCol7 +" TEXT," + questionCol8 +" TEXT)");

        db.execSQL("CREATE TABLE "+ playedQuizzes +"("+playedQuizCol1+" INTEGER PRIMARY KEY,"+
                playedQuizCol2+" TEXT,"+ playedQuizCol3 +" TEXT,"+ playedQuizCol4 +" TEXT," +
                playedQuizCol5 + " TEXT)");
        System.out.println("this is onCreate method");
        InsertQuiz();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+questionTable);
        db.execSQL("DROP TABLE IF EXISTS "+playedQuizzes);

        onCreate(db);
    }

In my Activity subclass i have called the constructor of above class using

 public void onStart(){
        try{
            super.onStart();
             dbHelper = new DatabaseHelper(this);
        }catch(Exception ex){
            CatchError(ex.toString());
        }       
    }

But when i run my Activity class and open my DDMS perspective then i did not find any database created under /data/data/

Is there something wrong with my knowlege or my code? If yes please tell me.

Upvotes: 1

Views: 562

Answers (2)

SilentKiller
SilentKiller

Reputation: 6942

In your code you didn't open the database....

to create, write or open database getWritableDatabase()

or to create & open the database getReadableDatabase() is necessory.....

Upvotes: 1

Flo
Flo

Reputation: 27455

From the documentation of the SQLiteOpenHelper class:

Create a helper object to create, open, and/or manage a database. This method always returns very quickly. The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.

So the database doesn't get created before not calling getWritableDatabase() or getReadableDatabase().

Upvotes: 0

Related Questions