什么是SQLiteOpenHelper类上下文参数呢? [英] What does the SQLiteOpenHelper class do with the context parameter?

查看:510
本文介绍了什么是SQLiteOpenHelper类上下文参数呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我延长了 SQLiteOpenHelper 类。我的构造函数是

I am extending the SQLiteOpenHelper class. My constructor is

public MyDatabaseHelper(Context context) {
    super(
        context,         // ???
        "MyDatabase.db", // Database name
        null,            // Cursor factory
        1                // database version
    );
}

什么是SQLiteOpenHelper的构造做的上下文信息?

What does the SQLiteOpenHelper constructor do with the context information?

有关我的应用程序,构造函数的行为一样的,不管程序的状态(上下文)的。我可以通过空的情况下与任何未来的问题?

For my application, the constructor will behave the same regardless of the program state (context). Can I pass null in for the context with out any future problems?

推荐答案

如果你提供一个空值,它会创建一个内存中数据库,而不是,但你需要提供空数据库名称参数,以及因此它的妥善处理。

If you supply a null value, it will create an in-memory database instead but you'll need to supply null for the database name parameter as well so it's handled properly.

这是记录在案的构造文档上下文

This is documented in the constructor documentation for context

上下文用来打开或创建数据库的数据库名   文件或空为内存数据库

context to use to open or create the database name of the database file, or null for an in-memory database

另外,如果您查看SQLiteHelper类本身的来源$ C ​​$ C,你会看到它使用MNAME值来决定是否使用mContext。查看源代码code网上的位置:

Also, if you view the source code of the SQLiteHelper class itself, you will see it uses the mName value to decide whether to use mContext. View the source code online here:

<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.4_r1.2/android/database/sqlite/SQLiteOpenHelper.java#SQLiteOpenHelper.0mContext">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.4_r1.2/android/database/sqlite/SQLiteOpenHelper.java#SQLiteOpenHelper.0mContext

这篇关于什么是SQLiteOpenHelper类上下文参数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆