简体   繁体   English

我调用onUpgrade和onCreate时未运行

[英]onUpgrade and onCreate doesn't running when i called it

I have a problem with SQLite, it doesn't work when i call DataBaseHelper(who store the onCreate and onUpgrade method), even if the version of DataBase change, and i have no errors. 我在使用SQLite时遇到问题,即使我更改了DataBase的版本,但我调用DataBaseHelper(存储onCreate和onUpgrade方法)也无法正常工作,并且没有错误。

For resuming my application, my main activity call the Singleton, who execute DataBaseHelper. 为了恢复我的应用程序,我的主要活动叫Singleton,他执行DataBaseHelper。

My main Activity : 我的主要活动:

public class irdesApplication extends Activity {

public static final String LOG_TAG="Droidnova";
private Context context = this;
CheckUpdate chUp;
ProgressBar bar;
TextView txtLoading;
Data d = new Data();
Singleton s;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.page_loading);
    bar = (ProgressBar) findViewById(R.id.ProgressBarLoad);
    txtLoading = (TextView) findViewById(R.id.txtLoading);
    new Thread(myThread).start();
    bar.setProgress(0);

    //Initialisation de la Base
    s.getInstance(context);

    // ---------- Constitution des logs  ------------ //

    int logParam =0;
    Log.v(LOG_TAG, "logParam value" + logParam);
    [...]

}

} }

My Singleton : 我的单身人士:

package com.irdes.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class Singleton {

private static Singleton instance;
private DataBaseHelper baseIrdes;
private SQLiteDatabase db;
private Data d = new Data();

private Singleton(Context context){
    System.out.println("Construction du Singleton");
    d.loadPercent = 10;
    d.loadTxt = "Initialisation";
    baseIrdes = new DataBaseHelper(context, d.dbName, null, d.dbVersion);
}

public static synchronized Singleton getInstance(Context context){
        if (instance == null)
        instance = new Singleton(context);
        return instance;
}

public void setValue(SQLiteDatabase value){
    db = value;
}
public SQLiteDatabase getValue(){
    return db;
}

protected void open(){
    //on ouvre la BDD en écriture
    db = baseIrdes.getWritableDatabase();
}

protected void close(){
    //on ferme l'accès à la BDD
    db.close();
}

} }

My DataBasHelper Activity : 我的DataBasHelper活动:

public class DataBaseHelper extends SQLiteOpenHelper {

private Data d = new Data();

public DataBaseHelper(Context context, String name, CursorFactory factory, int version) {
    super(context, name, factory, version);
    System.out.println("Test");
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {

    d.updateDataBase = true;
    //Activation prise en charge Foreign Keys
    db.execSQL("PRAGMA foreign_keys = ON;");

    [...]
    db.execSQL("CREATE TABLE "+d.actualiteTable+" ("+d.colActualiteNum+ " INTEGER PRIMARY KEY , "+
            d.colActualiteTitre+ " TEXT, "+d.colActualiteDate+" TEXT, "+d.colActualiteLien+" TEXT, "+d.colActualiteTypeActu+" INTEGER, " +
                    "FOREIGN KEY ("+d.colActualiteTypeActu+") REFERENCES "+d.typeActuTable+" ("+d.colTypeActuNum+"))");
    [...]
      System.out.println("Create table Complete");
      InsertRows(db);
}



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    [...]
    db.execSQL("DROP TABLE IF EXISTS "+d.actualiteTable);
    [...]

    onCreate(db);

}

public void InsertRows (SQLiteDatabase bdd){

    System.out.println("Chargement des données");

    bdd = this.getWritableDatabase();
    ContentValues cv=new ContentValues();

    // --------- TypeActu -------- //

    cv.put(d.colTypeActuNum, 1);
    cv.put(d.coltypeActuLibelle, "Gen");
    bdd.insert(d.typeActuTable, d.colTypeActuNum, cv);
    [...]
    bdd.close();
    System.out.println("Données inserées");

} }

And Data class for more comprehension : 和数据类以获得更多理解:

public static final String dbName="irdesDB";
public static final int dbVersion=46;

public static final String actualiteTable="Actualite";
public static final String colActualiteNum="numActu";
public static final String colActualiteTitre="titre";
public static final String colActualiteDate="date";
public static final String colActualiteLien="lien";
public static final String colActualiteTypeActu="numTypeActu";
[...]

I have aslo posted in a french forum here, if it could help: http://www.developpez.net/forums/d1110144/java/general-java/java-mobiles/android/sqlite-onupgrade-ne-s-execute-plus/#post6141983 如果有帮助,我也会在这里的法语论坛上发布aslo: http : //www.developpez.net/forums/d1110144/java/general-java/java-mobiles/android/sqlite-onupgrade-ne-s-execute -plus /#post6141983

What's the problem ? 有什么问题 ? I don't understand why onCreate and onUpgrade doesn't executed. 我不明白为什么不执行onCreate和onUpgrade。

Those methods are only called when you call getWritableDatabase() or getReadableDatabase() . 仅在调用getWritableDatabase()getReadableDatabase()时才调用这些方法。

In your case, invoke the baseIrdes. 在您的情况下,请调用baseIrdes。 open() method. open()方法。

The same problem was address here . 同样的问题也在这里解决。

According to the API.. 根据API。

public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

Since: API Level 1 由于:API级别1

Create a helper object to create, open, and/or manage a database. 创建一个辅助对象以创建,打开和/或管理数据库。 This method always returns very quickly. 此方法总是很快返回。 The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called. 在调用getWritableDatabase()或getReadableDatabase()之一之前,实际上不会创建或打开数据库。

So you have to make sure you run getReadableDatabase() or getWritableDatabase() . 因此,您必须确保运行getReadableDatabase()getWritableDatabase() I see you have getWritableDatabase() in your open() method, but I don't see where you are running your open method? 我看到您的open()方法中有getWritableDatabase() ,但看不到您在何处运行您的open方法? I have not used a database helper personally, so there could be something I'm unaware of, but I don't see where your open() is executed at all.. 我个人没有使用过数据库助手,所以可能有些我不知道的东西,但是我根本看不到open()在哪里执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM