SQLite的DatabaseHelper类 [英] SQLite DatabaseHelper class

查看:2149
本文介绍了SQLite的DatabaseHelper类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们创建一个Android应用程序可以使用DatabaseHelper类,它扩展SQLiteOpenHelper SQLite数据库。

when we create SQLite database from an android application can use a DatabaseHelper class which extend SQLiteOpenHelper.

我的构造函数如下:

public DatabaseHelper(Context context)  
{
      super(context, dbName, null, DATABASE_VERSION);  
}

随后的onCreate()方法

Then the onCreate() method

public void onCreate(SQLiteDatabase db) 
{
    String qry = "CREATE TABLE DEPT(dept_id INTEGER PRIMARY KEY, deptName TEXT)";   
    db.execSQL(qry);
    InsertDepts(db); 
}

但是当我创建DatabaseHelper类的一个实例,我的数据库还没有创建呢。

But when i create an instance of the DatabaseHelper class, my database havent created yet.

我相信,当我创建一个对象,从这个类的构造函数执行,因为没有建立但它会创建数据库并执行的onCreate()方法的数据库。所以,绝对是一个数据库,必须创建。

I believe when I create an Object from this class, the constructor executes and since no database created yet it will create database and execute onCreate() method. So definitely an database must be created.

或者直到我们执行下列code没有它执行的onCreate()方法,即使我们创建的类的对象。

Or untill we execute the following code doesn't it execute the onCreate() method, even if we create an object from the class.

databaseHelper.getWritableDatabase()

任何人能请解释一下databasehelper类的执行顺序。

Can anyone please explain the execution order of the databasehelper class.

推荐答案

您创建 DatabaseHelper 级的一个实例,这将导致执行构造函数。构造函数将执行母类构造函数( SQLiteOpenHelper ),这将然后检查数据库文件已经存在。

You create an instance of your DatabaseHelper-class, which causes the constructor to be executed. The constructor will execute the mother-classes constructor (SQLiteOpenHelper), which will then check if the Database-file already exists.

如果它存在,不采取任何措施,并创建你的对象。

If it exists, nothing is done and your object is created.

如果它不存在,数据库文件被创建,的onCreate 方法会被调用。

If it doesn't exist, the Database-file is created and the onCreate-method is called.

您说,您的数据库不存在。您是否获得在LogCat中的任何错误?另外,你有没有尝试过使用亚行访问数据库?

You say, your Database doesn't exist. Are you getting any errors in the LogCat? Also, have you tried accessing the Database using the adb?

这篇关于SQLite的DatabaseHelper类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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