2011-05-13 59 views
5

我真的很苦恼在我的应用程序中使用SqlLite;Android SQLite何时何地初始化SQLiteOpenHelper

我已经建立了从SQLiteOpenHelper

延伸
public class DatabaseHelper extends SQLiteOpenHelper { 

在OnCreate我创造约6表的数据库辅助类:

public void onCreate(SQLiteDatabase db) { 
db.execSQL("CREATE TABLE myTestTable (Id INTEGER PRIMARY KEY , " 
      + "info TEXT," + 
      + "TopLeft TEXT")); <-- example insert 

} 

当我的应用程序开始我解析一些XML和将数据输入到表中,并且检查了数据库,并且所有数据都在那里,我的表格正确创建。

当我去打电话的数据它不见了! 我在dbhelper类和每个活动中都有很多不同的查询;我正在重新初始化类,以便能够在类中获得我的函数。

我认为这不是做它的方式,因为它再次运行我的oncreate并擦除我所有的表(或者至少这是当我尝试调用数据后检查数据库时正在执行的操作它和它是空的!

在我印象中的OnCreate只跑了一次,下,但这显然并非如此,因为这似乎是重建我的DB每次擦着我的数据!

你在哪里意思初始化dbhelper类,并且如何阻止它重新创建表?我需要数据在应用程序关闭时仍然存在!

我很困惑!

如果这没有意义,请问我如何澄清它的具体问题!

UPDATE

我发现我可以添加

CREATE TABLE IF NOT EXISTS 

停靠它重新创建表,但它似乎仍然没有正确调用的OnCreate每次..

+0

很好的问题,我希望有人想出了一个答案,至于你的“插入”不过,你标志着关闭数据库之前成功的交易? – 2011-05-13 15:37:36

+0

是否将静态类声明为静态解决问题? – 2011-05-13 15:42:09

+0

@DAVE G所有的数据肯定会进入数据库,我的意思是在插入后关闭数据库?目前只是在做:\t SQLiteDatabase db = this.getWritableDatabase(); ....整理所有的数据和做:db.insert(“TABLENAME”,null,cv); ...那就是它的结局? – Bex 2011-05-13 15:44:36

回答

2

你可以在它周围放一个包装类?

public class DbAdapter 
{ 

    // database details 
    private static final String DATABASE_NAME = "dbname"; 
    private static final int DATABASE_VERSION = 1; 

    // where we're running 
    private final Context mCtx; 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
    DatabaseHelper(Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) 
    { 
     // your initial database create statements go here 
     db.execSQL(DATABASE_CREATE); 
    } 

    // if you need to recreate the database, you just up the version number 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     // drop commands for each table goes here 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
    } 
    } 

    public DbAdapter(Context ctx) 
    { 
    this.mCtx = ctx; 
    } 

    public DbAdapter open() throws SQLException 
    { 
    mDbHelper = new DatabaseHelper(mCtx); 
    mDb = mDbHelper.getWritableDatabase(); 
    return this; 
    } 

    public void close() 
    { 
    mDbHelper.close(); 
    } 

    // other database methods can go here 
} 
0

我不确定你的意思是什么 -

“每个活动我; m重新启动类,所以我可以得到我在课堂上的作用。“

在您需要访问数据库的每个活动中,您只需打开数据库,访问/更新表中的数据并关闭处理程序。

你见过这个简单的示例记事本程序 -

NotePad sample app by Google

可能,这将有助于

1

onCreate()方法被调用仅在第一次尝试访问你的数据库,它不是创造。所以onCreate()只会被调用一次。 getWritableDatabase返回一个数据库对象,如有需要将调用onCreate()NotePadProvider举例说明了如何使用SQLiteOpenHelper

+0

当你第一次访问数据库时说,你的意思是永远或在当前的“会话”? – Bex 2011-05-13 16:30:55

+0

我的意思是...... – 2011-05-13 16:38:37

+0

记事本示例应用程序的链接已损坏。任何想法它移动? – syonip 2016-02-27 16:28:33

0

数据库初始化代码应该在你的助手的onCreate方法。 从Android SQLiteOpenHelper的javadoc:

/** 
* Called when the database is created for the first time. This is where the 
* creation of tables and the initial population of the tables should happen. 
* 
* @param db The database. 
*/ 
public abstract void onCreate(SQLiteDatabase db);