2

I've seen a lot of ties between the onCreate and onOpen methods and SQLite database management in Android.

I am an iOS developer and I'm trying to "translate" (so to speak) my cocoa library so it could be used on Android. I need to create an SQLite database at runtime. I don't have an activity - since this is a library I'm creating. It seems I can't create a DB without an activity, is this correct ?

0

2 Answers 2

1

To create sqlite db, you don't basically need an activity in the library.it needs a context!!, You can have a method in library/class which basically take a context in input/parameter and create database.

This context can be passed from application activity or service or receiver.

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

4 Comments

So how can I cater for this? My needs are very simple, I have a Database class and the first thing I need is it open/create a database. Where can I get a context from? Knowing that I'm trying to create a library for others to use. It feels like I need to create an entire android app to get this working ?
no, You did not get my point, database will be created at the first call to constructor sqliteopenhelper, where you can get a context in the constructor parameter. yes the db will only be created , when an application calls ur library!!
Of course that's great. Will look into this sqliteopenhelper. I got the impression it was a third party helper class and I need to do everything from scratch
Yes you need to create a class which must extends SQLiteOpenHelper, then the constructor will need a context as argument, which basically need to creat database. TO open database for read/write you will need to call this.getWritableDatabase(); or readable.
0

I don't see why you should not be able to open a database without an activity. You need to extend SQLiteOpenHelper.

public class MyDatabase {
private final DatabaseHelper databaseHelper;
private SQLiteDatabase db;
private static final String DATABASE_NAME = "com.my.db";
private static final int DATABASE_VERSION = 1;

public MyDatabase(Context context) {
    databaseHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {
                //use db.execSQL to create the database
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
               // use db.execSQL to update (modify) the database
    }
}

public SQLiteDatabase open() throws SQLException {
    db = databaseHelper.getWritableDatabase();
    return db;
}

}

1 Comment

as context you can pass your application context in case you want from the application that will use the library

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.