Moraki
Moraki

Reputation: 243

Android SQLite database not retaining it's values every time I open the Android Virtual Device

I can create the database just fine, I can Insert and view the values, but whenever I close the android virtual device in eclipse and reopen it, the database values are gone! :O Please help me!

There are 3 classes : (1 that handles the database and 2 activities)

It would mean the world to me!!! :O I'd +rep if I coudl Thank you guys sooo much!@!!!

Here is my code

Database manager :`

package com.jeux;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class Database2 {

private static final String DATABASE_NAME = "JeuxMotActivity";

private static final int DATABASE_VERSION = 1;

public static final String TABLE_CATEGORIE = "Categorie";
public static final String KEY_CATEGORIE_ID_CATEGORIE = "IDCategorie";
public static final String KEY_CATEGORIE_NOM_CATEGORIE = "NomCategorie";

public static final String TABLE_MOT = "Mot";
public static final String KEY_MOT_ID_MOT = "IDMot";
public static final String KEY_MOT_MOT = "Mot";
public static final String KEY_MOT_EMAIL = "Email";
public static final String KEY_MOT_ID_CATEGORIE = "IDCategorie";

public static final String TABLE_ADMINISTRATEUR = "Administrateur";
public static final String KEY_ADMINISTRATEUR_EMAIL = "Email";
public static final String KEY_ADMINISTRATEUR_PASSWORD = "Password";


public static final String TABLE_SCORE = "Score";
public static final String KEY_SCORE_ID_JEU = "IDJeu";
public static final String KEY_SCORE_USERNAME = "Username";
public static final String KEY_SCORE_SCORE = "Score";
public static final String KEY_SCORE_INITIALES = "Initiales";

public static final String TABLE_UTILISATEUR = "Utilisateur";
public static final String KEY_UTILISATEUR_USERNAME = "Username";
public static final String KEY_UTILISATEUR_PASSWORD = "Password";

public static final String TABLE_JEU = "jeu";
public static final String KEY_JEU_ID_JEU = "IDJeu";
public static final String KEY_JEU_NOM_JEU = "NomJeu";
public static final String KEY_JEU_COMMENTAIRES = "Commentaires";


private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;


private static class DBHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(android.database.sqlite.SQLiteDatabase nb) {
        //TODO check teh constraints and the relations
        nb.execSQL("CREATE TABLE " + TABLE_CATEGORIE + " (" +
                KEY_CATEGORIE_ID_CATEGORIE + " INTEGER PRIMARY KEY    AUTOINCREMENT, " +
                KEY_CATEGORIE_NOM_CATEGORIE + " TEXT NOT NULL" +     ");"
                );

        nb.execSQL("CREATE TABLE " + TABLE_MOT + " (" +
                KEY_MOT_ID_MOT + " INTEGER PRIMARY KEY     AUTOINCREMENT, " +
                KEY_MOT_MOT + " TEXT NOT NULL," + 
                KEY_MOT_EMAIL + " TEXT NOT NULL," + 
                KEY_MOT_ID_CATEGORIE + " TEXT NOT NULL," + 
                "FOREIGN KEY(" + KEY_MOT_ID_CATEGORIE + ") REFERENCES " + TABLE_CATEGORIE + "(" + KEY_CATEGORIE_ID_CATEGORIE  + ")," +
                "FOREIGN KEY(" + KEY_MOT_EMAIL + ") REFERENCES " + TABLE_ADMINISTRATEUR + "(" + KEY_ADMINISTRATEUR_EMAIL  + ")" +
                ");"
                );

        nb.execSQL("CREATE TABLE " + TABLE_ADMINISTRATEUR + " (" +
                KEY_ADMINISTRATEUR_EMAIL + " TEXT PRIMARY KEY , " +
                KEY_ADMINISTRATEUR_PASSWORD + " TEXT NOT NULL" +     ");"
                );

        nb.execSQL("CREATE TABLE " + TABLE_SCORE + " (" +
                KEY_SCORE_ID_JEU + " TEXT NOT NULL, " +
                KEY_SCORE_USERNAME + " TEXT NOT NULL," + 
                KEY_SCORE_SCORE + " INTEGER NOT NULL," + 
                KEY_SCORE_INITIALES + " TEXT NOT NULL," + 
                "FOREIGN KEY(" + KEY_SCORE_ID_JEU + ") REFERENCES "      + TABLE_JEU + "(" + KEY_JEU_ID_JEU  + ")," +
                "FOREIGN KEY(" + KEY_SCORE_USERNAME + ") REFERENCES      " + TABLE_UTILISATEUR + "(" + KEY_UTILISATEUR_USERNAME  + ")" +
                ");"
                );

        nb.execSQL("CREATE TABLE " + TABLE_UTILISATEUR + " (" +
                KEY_UTILISATEUR_USERNAME + " TEXT PRIMARY KEY , " +
                KEY_UTILISATEUR_PASSWORD + " TEXT NOT NULL" + ");"
                );
        nb.execSQL("CREATE TABLE " + TABLE_JEU + " (" +
                KEY_JEU_ID_JEU + " INTEGER PRIMARY KEY     AUTOINCREMENT, " +
                KEY_JEU_NOM_JEU + " TEXT NOT NULL UNIQUE," + 
                KEY_JEU_COMMENTAIRES + " TEXT" + 
                ");"
                ); 
    }

    @Override
    public void onUpgrade(android.database.sqlite.SQLiteDatabase db,
            int oldversion, int newversion) {
    //  db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORIE);
    //  onCreate(db);
    }

}

    public Database2(Context c){
        ourContext = c;
    }

    public Database2 open() throws SQLException {
        ourHelper = new DBHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close() throws SQLException{
        ourHelper.close();
    }       

    public long createCategorie(String name) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_CATEGORIE_NOM_CATEGORIE, name);
        return ourDatabase.insert(TABLE_CATEGORIE, null, cv);   
    }

    public String getCategorieData(){
        String result = "";
        String[] columns = new String[] {KEY_CATEGORIE_ID_CATEGORIE,     KEY_CATEGORIE_NOM_CATEGORIE};
        Cursor c = ourDatabase.query(TABLE_CATEGORIE, columns,null, null,     null, null, null);

        int id = c.getColumnIndex(KEY_CATEGORIE_ID_CATEGORIE);
        int name = c.getColumnIndex(KEY_CATEGORIE_NOM_CATEGORIE);

        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result += c.getString(id) + " " + c.getString(name) + "\n";
        }

        return result;
    }

    }

`

Event Manager :



package com.jeux;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Jeuxdemots2Activity extends Activity implements OnClickListener {


  Button sqlUpdate, sqlView;
  EditText nameText;

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  sqlUpdate = (Button) findViewById(R.id.updatebutton);
  sqlView = (Button) findViewById(R.id.afficherbutton);
  nameText = (EditText) findViewById(R.id.nametext);

  sqlUpdate.setOnClickListener(this);
  sqlView.setOnClickListener(this);    
  }

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch (arg0.getId()){
    case R.id.updatebutton:
        try{
    String name = nameText.getText().toString();
    Database2 entry = new Database2(Jeuxdemots2Activity.this);
    entry.open();
    entry.createCategorie(name);
    entry.close();
        }catch (Exception e){
            String error = e.toString();

            Dialog d = new Dialog(this);
            d.setTitle("Dang it!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }finally{

        }


    break;

    case R.id.afficherbutton:
        Intent i = new Intent("android.intent.action.SQLVIEW");
        startActivity(i);
    break;

    }


}
}

`

view #3 :

package com.jeux;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SQLview extends Activity {

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.afficherlayout);
    TextView tv = (TextView) findViewById(R.id.affichertextview);
    Database2 info = new Database2(this);
    info.open();
    String data = info.getCategorieData();
    info.close();
    tv.setText(data);
}
}

A photo to put context to my situation

Upvotes: 3

Views: 845

Answers (1)

Gophermofur
Gophermofur

Reputation: 2101

When you close/re-open your virtual device, does it look like it's doing a full bootup every time (Shiny Android word, swipe to unlock screen, etc)? If so, it could be re-initializing everything everytime you run your application. If so, you're application and database are no longer on the device, so the data wouldn't persist.

When you create your emulator are you checking the "Snapshot" option? I believe that saves the state of your device into a file so that when you close/re-open your emulator, it's able to resume from the same point.

Upvotes: 5

Related Questions