Can't insert new data into SQLite database after closing the app

GreeNSpartGreeNSpart FRMember
edited February 2017 in Xamarin.Android

Hello !
I'm creating a little application to rate movies for a project in school, and i have a problem. The first time that i launch the application i don't have any issues, i can wrote the name of the movie i want to rate and then it's insert in the database. Here is the code of my database:
`class FilmDB : SQLiteOpenHelper
{
SQLiteDatabase db;
public static readonly string create_table_sql = "CREATE TABLE[films]([_id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, [titre] TEXT NOT NULL, [note] REAL NOT NULL)";
public static readonly string Database_Name = "films.db";
public static readonly int Database_Version = 1;

    public FilmDB(Context context):base(context, Database_Name, null, Database_Version) { }

    public override void OnCreate(SQLiteDatabase db)
    {

        this.db = db;
        db.ExecSQL(create_table_sql);

    }

    public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        throw new NotImplementedException();
    }

    public void InsertFilm(string titre, float note)
    {
        ContentValues values = new ContentValues();
        values.Put("titre", titre);
        values.Put("note", note);
        db.Insert("films",null, values);
    }
}`

When i close the app and launch it again i have an error when it comes to insert the data because db is null.

here the code of my main activity :
`[Activity(Label = "AppliAvecBDD", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{

    ICursor cursor;
    FilmDB filmDB;
    private const int CODE_UNEAUTREACTIVITE = 1;
    string film = "";
    Button btnNoter;
    EditText monFilm;
    ListView list;
    FilmCursorAdapter adapter;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        filmDB = new FilmDB(this);
        cursor = filmDB.ReadableDatabase.RawQuery("Select * from films", null);
        StartManagingCursor(cursor);

        btnNoter = FindViewById<Button>(Resource.Id.btnNoter);
        monFilm = FindViewById<EditText>(Resource.Id.film);
        list = FindViewById<ListView>(Resource.Id.listView);

        adapter = new FilmCursorAdapter(this, cursor, false);
        list.Adapter = adapter;

        btnNoter.Click += delegate
        {
            film = monFilm.Text;
            Intent intent = new Intent(this, typeof(Note));
            intent.PutExtra("film", film);
            StartActivityForResult(intent, CODE_UNEAUTREACTIVITE);
            StopManagingCursor(cursor);
        };
    }
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        switch (requestCode)
        {
            case CODE_UNEAUTREACTIVITE:
                switch (resultCode)
                {
                    case Result.Ok:
                        string titre = data.GetStringExtra("leFilm");
                        float note = data.GetFloatExtra("note", 0);
                        filmDB.InsertFilm(titre, note);
                        cursor = filmDB.ReadableDatabase.RawQuery("Select * from films", null);
                        adapter.ChangeCursor(cursor);
                        StartManagingCursor(cursor);
                        break;
                    default:
                        break;
                }
                break;
            default: break;
        }
    }
}`

EDIT: I know that StartManagingCursor is deprecated but our teacher wants us to use it for this app :neutral:

Best Answer

  • GreeNSpartGreeNSpart FR
    Accepted Answer

    Nevermind i find the answer to my question :)

    When i run my app for the second time, it didn't go into onCreate() so i had to create a constructor instead of leaving it empty :

    ` public FilmDB(Context context):base(context, Database_Name, null, Database_Version) {

            db = base.WritableDatabase;
        }`
    

    Sorry for my bad english :smile:

Answers

  • GreeNSpartGreeNSpart FRMember
    Accepted Answer

    Nevermind i find the answer to my question :)

    When i run my app for the second time, it didn't go into onCreate() so i had to create a constructor instead of leaving it empty :

    ` public FilmDB(Context context):base(context, Database_Name, null, Database_Version) {

            db = base.WritableDatabase;
        }`
    

    Sorry for my bad english :smile:

Sign In or Register to comment.