gpt4 book ai didi

java - 当我调用 onUpgrade 和 onCreate 时,它​​没有运行

转载 作者:行者123 更新时间:2023-12-02 00:39:21 24 4
gpt4 key购买 nike

我的SQLite有问题,当我调用DataBaseHelper(存储onCreate和onUpgrade方法)时它不起作用,即使DataBase的版本发生变化,并且我没有错误。

为了恢复我的应用程序,我的主要 Activity 调用 Singleton,它执行 DataBaseHelper。

我的主要 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);
[...]

}

}

我的单例:

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();
}

}

我的 DataBasHelper Activity :

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");

}

还有数据类以便更好地理解:

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";
[...]

我也在这里的法国论坛上发帖,如果有帮助的话:http://www.developpez.net/forums/d1110144/java/general-java/java-mobiles/android/sqlite-onupgrade-ne-s-execute-plus/#post6141983

有什么问题吗?我不明白为什么 onCreate 和 onUpgrade 不执行。

最佳答案

这些方法仅在您调用 getWritableDatabase()getReadableDatabase() 时调用。

在您的情况下,调用 baseIrdes.open() 方法。

同样的问题是地址 here .

关于java - 当我调用 onUpgrade 和 onCreate 时,它​​没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6817919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com