Problems reading an sqlite database

2

Hello, I have the following code that calls the database of my application. All I need is to count the amount of data. I AM ROOT USER

    public int contar() throws SQLException{

    Cursor mcursor = null;
    mcursor = this.getReadableDatabase().query("Contacts",new String[]{"_id"}, null ,null,null,null,null );


    return mcursor.getCount();

}

but when consulting the base I get this error ... That never happened to me and it is the first time. I have already given the respective permits to the base

  

05-11 00: 48: 59.313 25496-25496 / conts.com.contacts D / ViewRootImpl: ViewPostImeInputStage processPointer 0

05-11 00:48:59.493 25496-25496/conts.com.contactos D/ViewRootImpl: ViewPostImeInputStage processPointer 1

05-11 00:48:59.523 25496-25496/conts.com.contactos D/SecWifiDisplayUtil: Metadata value : SecSettings2

05-11 00:48:59.533 25496-25496/conts.com.contactos D/ViewRootImpl: #1 mView = android.widget.LinearLayout{96c1df6 V.E...... ......I. 0,0-0,0 #10203a4 android:id/toast_layout_root}

05-11 00:48:59.553 25496-26587/conts.com.contactos W/FileUtils: Failed to chmod(/data/data/com.miapp/databases/MIBASE.db): android.system.ErrnoException: chmod failed: EPERM (Operation not permitted)

05-11 00:48:59.553 25496-26587/conts.com.contactos E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1

Process: conts.com.contactos, PID: 25496

android.database.sqlite.SQLiteException: Can't downgrade database from version 28 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.getReadableDatabase(SQLiteOpenHelper.java:187)

    at contwhats.com.contactoswhatsapp.DeveloperBD.contar(DeveloperBD.java:61)

    at contwhats.com.contactoswhatsapp.MainActivity$1.run(MainActivity.java:55)

    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)

    05-11 00:48:59.563 25496-25708/conts.com.contactos D/mali_winsys: new_window_surface returns 0x3000,  [328x104]-format:1

    05-11 00:48:59.643 25496-25496/conts.com.contactos D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1

    05-11 00:49:00.093 25496-25708/conts.com.contactos D/OpenGLRenderer: endAllActiveAnimators on 0xf3734280 (RippleDrawable) with handle 0xe2193820

    05-11 00:49:00.443 25496-25496/conts.com.contactos D/ViewRootImpl: #3 mView = null

    05-11 00:49:03.043 25496-25496/conts.com.contactos D/ViewRootImpl: #3 mView = null

If you can give me a piece of code to save this error, I do not know what the problem is. Greetings Lina

    
asked by Fante Es aprender 11.05.2018 в 06:04
source

1 answer

-1

The message does not have to do with permissions, it is because you created a database with a higher version than the one you are trying to update in the SQLitOpenHelper constructor.

Currently your code tries to update to a new version, but this one is smaller than the one that your application has, you have these options:

  
  • Whether it is a test or a development version, simply delete the cache or delete the application and upload the application back to your device.
  •   
  • If it is a production application, assign version 29, since you are detecting you have created a database with version 28.
  •   

To change to a new version this is an example:

public class MyDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "myDatabase";

    private static final int DATABASE_VERSION = 29;

    public MyDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
     ...
     ...
    
answered by 11.05.2018 в 17:10