0

I'm using SQLite to create a database table for my app. I've searched online but haven't found the answer yet: Do I need to create a database first or is there a default database for each app?

I've written the following DBHelper class. It's in a separate file. How Do I call it when the app starts?

public class DataBaseHelper extends SQLiteOpenHelper{

    final String CREAT_TABLE = "CREATE TABLE IF NOT EXIST `employee` ("+
    "`id` int(11) NOT NULL AUTO_INCREMENT,"+
    "`firstName` varchar(30) NOT NULL,"+
    "`lastName` varchar(30) NOT NULL,"+  
    "PRIMARY KEY (`id`)"+
    ") ;";

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREAT_TABLE);
    }
}

2 Answers 2

2

You don't have to create the database yourself. You specify the database name (the sqlite file name) when you call the superconstructor of your open helper. This will create or update the database with that name when needed (depending on which version number you send in vs. the current version number meta data).

I don't know what your constructor looks like but let's say it looks like

public DatabaseHelper(final Context context) {
    super(context, "mydatabase", null, 0);
}

Then the SQLiteOpenHelper will create a database named "mydatabase" when you call getReadableDatabase() or getWritableDatabase(). It's all in the docs.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. How do I call this helper function to create the table in my main Activity which is in another file/class?
The helper takes care of the most of it. Just override public abstract void onCreate (SQLiteDatabase db) and create the table there. You might have yo create tables in onUpgrade if you want to support upgrading databases when you upgrade the app (if the db version increases). If you don't care about old data you can drop all tables and recreate them in onUpgrade. Just make sure you don't add tables without increasing the db version and handling it somehow. Otherwise you will get broken apps when people lack tables due to app upgrades.
I tried to add the following code to my main activity but it didn't work. DataBaseHelper myDbHelper = new DataBaseHelper(this);
Ok. But I can't possibly answer such a vague statement. How does it not work? Does it match the constructor?
My constructor looks like this public DataBaseHelper(Context context) { super(context, DB_NAME, null, 1); this.myContext = context; }. but it doesn't seem that the onCreate method is called when the main activity execute 'new DataBaseHelper()' statement. I set a breakpoint at the db.execSQL() statement but it was ignored.
1

You declare your database name in the constructor

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.