Search code examples
androidandroid-sqlite

android - sqlite getReadableDatabase() error


I am a beginner and I want to use SQLITE but I got error. this function is inside the main class:

public Cursor getData(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from weight where id="+id+"", null );
    return res;
 }

I imported the sqlite files:

import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

the errror is: The method getReadableDatabase() is undefined for the type Activity3

please help


Solution

    1. Implement a class that extends SQLiteOpenHelper, providing the required onCreate() and onUpgrade() lifecycle callbacks.

    2. Call getReadableDatabase() on your database helper object and not on the activity, e.g.

      SQLiteDatabaseHelper helper = new MyDatabaseHelper(this);
      SQLiteDatabase db = helper.getReadableDatabase();
      

    Documentation.