-1

I'm trying to insert values to the database on SQLite by SQLiteOpenHelper bur the app closes.

I'm working with SQLiteOpenHelper on Android studio, and using an activity to insert values in the database, when I first enter to the activity I can insert all the values I want, but when I go out, re-enter the activity and press the button "Registar" that insert the values on the table, the app closes without showing the Toast message.

public void Registrar (View view){

Toast.makeText(this,"Entro al registrar",Toast.LENGTH_SHORT).show();

    AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1);

    SQLiteDatabase bd = admin.getWritableDatabase(); <- in this line close

''''' ''''' }

THE LOG MODULE

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.appmediocurso, PID: 2740

java.lang.IllegalStateException: Could not execute method for android:onClick
    at $DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:6205)
    at android.widget.TextView.performClick(TextView.java:11103)
    at android.view.View$PerformClick.run(View.java:23653)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6205) 
    at android.widget.TextView.performClick(TextView.java:11103) 
    at android.view.View$PerformClick.run(View.java:23653) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6682) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

 Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 5 to 1
    at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:360)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:254)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)

    at com.example.appmediocurso.NuevoDeudor.Registrar(NuevoDeudor.java:57)

    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
    at android.view.View.performClick(View.java:6205) 
    at android.widget.TextView.performClick(TextView.java:11103) 
    at android.view.View$PerformClick.run(View.java:23653) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6682) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

E/Watchdog: !@Sync 4269 [26_abr_16_41_35.985] E/wifi: failed to get channel list : -95

I only expect to insert the values even if it's the second ot third time I enter to the actitity.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • `SQLiteException: Can't downgrade database from version 5 to 1` – Seems like, at some point, you instantiated `AdminSQLiteOpenHelper` with a version of 5, the last argument in the `SQLiteOpenHelper` constructor call. Generally, the version and database name are kept as constants in the `SQLiteOpenHelper` subclass, so you don't have to specify them each time you instantiate it, like you are in the given snippet – `AdminSQLiteOpenHelper(this, "administracion", null, 1)`. – Mike M. Apr 26 '19 at 22:36
  • Have a look at [this answer](https://stackoverflow.com/a/12015869) for an example of what I describe above. Notice `DATABASE_NAME` and `DATABASE_VERSION`, which are passed only in the `super` constructor call, and the subclass's constructor takes only a `Context` argument. You will want to re-create your database after the corrections, so you can either uninstall/reinstall your app, or select "Clear data" on your app's page in the device Settings. – Mike M. Apr 26 '19 at 22:48

1 Answers1

0

The cause is that before the failed run the database had had it's version number set at 5 but you are now trying to use version 1 without overriding the SQliteOpenHelper's onDowngrade method; which without being overridden results in the encountered exception, as is intended, as reducing the version number is generally not done.

That is AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 5); was used but now AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1); has been used (4th parameter was 5 but is now 1) .

You could fix this by again using 5 as the version number.

Another fix could be to uninstall the App or delete the App's data and then rerun the App without changing the version. Noting that this would result in all data being lost.

Yet another fix could be to override the onUpgrade method (perhaps doing nothing), the version should then be set to 1, the data would be kept.

There are other ways of fixing this issue but they would be a little more complicated and offer no advantage over the mentioned fixes.

MikeT
  • 51,415
  • 16
  • 49
  • 68