0

必要な 2 つのテーブル (user_table と prod_table) を使用してデータベース クラスをセットアップしています。

このガイドに従う: http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android

これはちょっと問題ないように見えますが、典型的な「getConnection()」メソッドをどのように行うかについては決して述べていません。たとえば、私はアクティビティの中にいて、insertUserRow() をしたいのですが、SQLiteOpenConnection db とユーザー オブジェクトを渡す必要があります。次のように、各ヘルパーでシングルトンを作成しました。

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;
    }

また、次のことを行うように指示した別の例をフォローアップしようとしました。

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);
    }

}

心に欠けているものがあります。

また、ヘルパーごとに UserEdbHelper と ProductEdbHelper の 2 つのクラスを作成しましたが、正しいですか? これらはそれぞれ独自のテーブルを作成し、独自のメソッドを持っています。(基本的には、上で追加したリンクと同じ構造です。そこで確認できます)

属性の 1 つは「dbName」です。その属性を両方のクラスに追加する必要がありますか、それとも 1 つだけに追加する必要がありますか?

ちょっと失った:(

少しでもお役に立てれば幸いです、

前もって感謝します、

マリアーノ。

4

1 に答える 1

2

何かを行うには、すべての間違った方法について心配するよりも、正しい方法について話す方がよいと私に言われました。質問のコードを確認する代わりに、うまくいくと思われるものを提案させてください。

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

}

あなたの言葉を引用してもよろしいですか?:-)

于 2013-03-20T01:13:40.397 に答える