SQLiteOpenHelper不是要求的onCreate [英] SQLiteOpenHelper not calling onCreate

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

问题描述

这是我用过SQLiteOpenHelper(或数据库上的机器人)的拳头时间。当我得到一个可写入数据库,我想知道为什么心不是的onCreate调用的类的每个新实例。难道我做错了什么?

This is the fist time I've used SQLiteOpenHelper (or databases on android). When I get a writeable database I was wondering why onCreate isnt being called on each new instance of the class. Am I doing something wrong?

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "MyDatabase.db";
    private static final int DATABASE_VERSION = 1;
    private String PrSQLcmd = "";


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


@Override
public void onCreate(SQLiteDatabase db) 
{
    db.execSQL("CREATE TABLE IF NOT EXISTS Contact(Firstname TEXT, LastName TEXT");
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

}

推荐答案

在SQLiteOpenHelper的的onCreate的含义是它是一个活动的不同。在这里,'的onCreate'只调用一次,这是你创建数据库的第一次。你运行应用程序下一次,该数据库已经存在,所以不会叫的onCreate。你的对象级初始化应在构造函数中,而不是在做'的onCreate

In SQLiteOpenHelper, the meaning of 'onCreate' is different from what it is in an Activity. Here,'onCreate' is called only once, which is the first time you create the database. The next time you run the app, the database is already there, so it won't call 'onCreate'. Your object level initialization should be done in the constructor and not in 'onCreate'

要见'的onCreate被调用,可以手动删除数据库文件,或者干脆卸载应用程序。

To see 'onCreate' being called, either manually delete the db file, or simply uninstall the app.

这篇关于SQLiteOpenHelper不是要求的onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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