msqar
msqar

Reputation: 3040

SQLiteOpenDatabase questions in android

i'm setting up my Database Classes with the 2 tables i need (user_table and prod_table)

Following this guide: http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android

Which looks kinda fine, but never says how do i do the typical "getConnection()" method. For instance, i'm inside an activity and i want to insertUserRow() i need to pass the SQLiteOpenConnection db and the user object. I've made a Singleton in each Helper like this:

public class UserEdbHelper extends SQLiteOpenHelper {

    static final String dbName = "edb";
    static final String userTable = "user_table";
    private static UserEdbHelper mInstance = null;

    /* --- user_table columns --- */

    static Context appContext;

    UserEdbHelper(Context context) {
        super(context, dbName, null, 33);
        this.appContext = context;
    }

    public static UserEdbHelper getInstance(Context ctx) {
        if (mInstance == null) {
            mInstance = new UserEdbHelper(ctx.getApplicationContext());
        }
        return mInstance;
    }

I've also tried to follow up another example which told to do the following:

public class AppCore extends Application{
    // create the 2 helpers that creates the respective tables
    public static UserEdbHelper userHelper; 
    public static ProductEdbHelper productHelper;

    public static void init(Context context )
    {
        // this is supposed to instantiate the helpers or what? what does this do?
        userHelper = new UserEdbHelper(context);
        productHelper = new ProductEdbHelper(context);
    }

}

I got things missing in my mind.

Also, i've created two classes for each Helper, UserEdbHelper and ProductEdbHelper, is that correct? each of these create their own table, and has their own methods. (Basically is the same structure as the link i've added above, you can check it out there)

One of the attributes is "dbName", do i need to add that attribute to both classes or only one?

Kinda lost :(

I would love to get some help,

Thanks in advance,

Mariano.

Upvotes: 0

Views: 72

Answers (1)

G. Blake Meike
G. Blake Meike

Reputation: 6705

It's been suggested to me that it is better to talk about the right way to do something than to worry about all the wrong ways. Instead of reviewing the code in your question, let me suggest something that I suspect will work for you:

public class AppCore extends Application {
    public UserEdbHelper userHelper;
    public ProductEdbHelper productHelper;

    // onCreate and such stuff...

    public SQLiteDatabase getUserDb() {
        if (null == userHelper) { userHelper = new UserEdbHelper(this); }
        return userHelper.getWritableDatabase();
    }

    public SQLiteDatabase getProductDb() {
        if (null == productHelper) { productHelper = new ProductEdbHelper(this); }
        return productHelper.getWritableDatabase();
    }

    // other code

}

Can I quote you: "I got things missing in my mind"? :-)

Upvotes: 2

Related Questions