Question

I'm trying to sync data between a webserver and an android app.So, i'm following this great advice: Sync data between Android App and webserver

Now, i'm actually working in the first part doing the Content Provider. For this, i found this tutorial: http://www.vogella.com/articles/AndroidSQLite/article.html

And in that tutorial they suggest that you should do one SQLiteOpenHelper for each table. It was working okay for me, but i realized that it was a bit odd, because i'll have one file for each table

Like this:

public class AppUserDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "appusertable.db";
private static final int DATABASE_VERSION = 1;

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

// Method is called during creation of the database
@Override
public void onCreate(SQLiteDatabase database) {
    AppUserTable.onCreate(database);
}
}

So, i started to do some research and i found out this: Should there be one SQLiteOpenHelper for each table in the database? and this link in particular: http://blog.foxxtrot.net/2009/01/a-sqliteopenhelper-is-not-a-sqlitetablehelper.html , and that makes a lot of sense to me.

Well, my problem is that actually it also makes a lot of sence to have one ContentProvider for each table, because if i don't it will be huge and very confusing, but in every ContentProvider is where i'm actually creating the database:

public class AppUserContentProvider extends ContentProvider {     
@Override
public boolean onCreate() {
    database = new AppUserDatabaseHelper(getContext());
    return false;
}
}

And this also gives me access to the database.

So my question is: I just should do a giant ContentProvider for all tables? or there is another way to create the database that should be shared between every ContentProvider?

I hope you can help me Thanks in advice

Was it helpful?

Solution

You should have a different content provider per resource but use one sqlliteopenhelper (1 database).

There are a few ways to do this. What I do (which may not be the best) it use a custom Application object in my app (http://developer.android.com/reference/android/app/Application.html), that has methods for accessing objects I want to be a singleton that also need a context. It might not be the best way but it works for me.

e.g.

public class MainApplication extends Application {
    public static DatabaseHelper databaseHelper;
    public static Context applicationContext;


    @Override
    public void onCreate() {
        super.onCreate();
        databaseHelper = new DatabaseHelper(this);
        applicationContext = getApplicationContext();
    }

    @Override
    public void onTerminate() {
        databaseHelper.close();
        super.onTerminate();
    }

}

DatabaseHelper is just my concrete implementation of SQLiteOpenHelper.

Define the custom application object in your Android manifest file.

<application android:name="com.example.MainApplication" ...>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top