Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android中的SQLiteOpenDatabase问题_Java_Android_Sqlite - Fatal编程技术网

Java android中的SQLiteOpenDatabase问题

Java android中的SQLiteOpenDatabase问题,java,android,sqlite,Java,Android,Sqlite,我正在用我需要的两个表(user\u表和prod\u表)设置我的数据库类 遵循本指南: 这看起来不错,但从来没有说明如何使用典型的“getConnection()”方法。 例如,我在一个活动中,我想要insertUserRow(),我需要传递SQLiteOpenConnection数据库和用户对象。 我在每个助手中创建了一个单件,如下所示: public class UserEdbHelper extends SQLiteOpenHelper { static final Strin

我正在用我需要的两个表(user\u表和prod\u表)设置我的数据库类

遵循本指南:

这看起来不错,但从来没有说明如何使用典型的“getConnection()”方法。 例如,我在一个活动中,我想要insertUserRow(),我需要传递SQLiteOpenConnection数据库和用户对象。 我在每个助手中创建了一个单件,如下所示:

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,对吗?每种方法都创建自己的表,并有自己的方法。 (基本上与我上面添加的链接结构相同,您可以在那里查看)

其中一个属性是“dbName”,我需要将该属性添加到两个类中还是只添加一个

有点失落:(

我很想得到一些帮助

提前感谢,


Mariano。

有人向我建议,与其担心所有错误的方法,不如讨论做某事的正确方法。与其回顾你问题中的代码,不如让我提出一些我认为对你有用的建议:

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

}

我可以引用你的话吗:“我脑子里缺少了一些东西”?:-)

哈哈哈+1。谢谢,那我怎么初始化数据库呢?我必须创建一个init()吗?等等+评论中有1,但答案中没有+1?菲!有很多代码展示了如何编写DbOpenHelper,例如,您需要两个:UserEdbHelper和ProductEdbHelper。