2

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

Community
  • 1
  • 1
ginobilicl
  • 23
  • 4
  • I am wondering why it suggests to have one `SQLiteOpenHelper` per table. How to do the query when you need to join say, two or three tables? – Lawrence Choy Oct 04 '13 at 01:33
  • I was wondering the same thing. I think most web tutorials or examples are somehow useless when you gotta do something for real. It's almost impossible to have an app with one table and isn't just like you got to repeat everything to work it out. – ginobilicl Oct 10 '13 at 22:20
  • i think this [post][1] will help you. [1]: http://stackoverflow.com/questions/13877701/sqliteopenhelper-one-for-each-table-in-database – telmo May 02 '14 at 16:17

1 Answers1

0

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" ...>
Lionel Port
  • 3,492
  • 23
  • 26
  • First of all, thanks for your answer. I was very doubtful about this and your answer helped me a lot. Also i found an concrete example for this: [link](http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html) Very interesting article. Greetings – ginobilicl Oct 10 '13 at 22:17