0

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.

For resuming my application, my main activity call the Singleton, who execute 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 :

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

What's the problem ? I don't understand why onCreate and onUpgrade doesn't executed.

toshiro92
  • 1,287
  • 5
  • 28
  • 42

2 Answers2

2

Those methods are only called when you call getWritableDatabase() or getReadableDatabase().

In your case, invoke the baseIrdes.open() method.

The same problem was address here.

Community
  • 1
  • 1
Kevin King
  • 1,549
  • 11
  • 14
1

According to the API..

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

Since: API Level 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.

So you have to make sure you run getReadableDatabase() or getWritableDatabase(). I see you have getWritableDatabase() in your open() method, but I don't see where you are running your open method? 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..

Ricardo
  • 7,785
  • 8
  • 40
  • 60
Joishi Bodio
  • 438
  • 6
  • 17
  • Looks like I was too late. (Loaded the page and spent a bunch of time looking at the code) *shrug* Glad the problem was solved. :) – Joishi Bodio Jul 25 '11 at 15:08