The method openOrCreateDatabase(String, int, null) is Undefined for the type Bancodedados

Asked

Viewed 2,314 times

0

I’m having trouble creating a class that creates and manipulates the database! But in the method criar() gave a problem saying that

"The method openOrCreateDatabase(String, int, null) is Undefined for the type Bancodedados".

Does anyone have any suggestions to get around such a problem?! My code follows below:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class BancoDeDados {
     private SQLiteDatabase db;
     private String DATABASE_NAME;
     private String TABLE_NAME;
     private String SQL_SELECT_ALL;
     private String SQL_SELECT_ID;
     private String SQL_CREATE;

     public BancoDeDados(SQLiteDatabase db,String DATABASE_NAME,String TABLE_NAME,String SQL_SELECT_ALL,String SQL_SELECT_ID,String SQL_CREATE){
         this.db = db;
         this.DATABASE_NAME = DATABASE_NAME;
         this.TABLE_NAME = TABLE_NAME;
         this.SQL_SELECT_ALL = SQL_SELECT_ALL;
         this.SQL_SELECT_ID = SQL_SELECT_ID;
         this.SQL_CREATE = SQL_CREATE;
     }

     public void criar(){
         this.db = openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
         this.db.execSQL(this.SQL_CREATE);
         this.db.close(); 
     }
}
  • You didn’t mean to say this.db.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);?

  • @bfavaretto No, but also tried that way and not to solve my problem! I found the solution using Context

2 answers

2

Are two mistakes.

The first is that you are trying to call a method that does not exist in the class BancoDeDados. That’s because BancoDeDados needs to be a subclass of SQLiteDatabase, or contain an instance (object) of the type SQLiteDatabase, as for example the object db, so you can call in some of the methods openOrCreateDatabase() available in this class (see documentation of Sqlitedatabase which are three ways to call this method). Are two ways to resolve this error: declare public class BancoDeDados extends SQLiteDatabase or else call db.openOrCreateDatabase(...) in place of openOrCreateDatabase(...).

The second mistake is that even if you can call the method openOrCreateDatabase(), the signature of this method is incorrect, i.e., the parameters passed to it do not match the types accepted by the method. You’re calling the method with the types String, int and null, and the accepted parameters are of other types.

One of the acceptable signatures receives a String, one SQLiteDatabase.CursorFactory and a DatabaseErrorHandler. Therefore, if you pass the parameters (String, null, null) might work. But don’t forget to resolve the first error before that.

  • Thank you for pointing out my "mistakes"! But to be honest I already knew all these "mistakes" before posting it here, I wanted some suggestion on how to use the method openOrCreateDatabase(...) da android.content.ContextWrapper.openOrCreateDatabase(String name, int mode, CursorFactory factory) which returns a type variable SQLiteDatabase. So the signature NÃO is wrong. 2º: Minha classe NÃO pode extender a classe SQLiteDatabase, Reason: >The type Bancodedados cannot subclass the final class Sqlitedatabase

  • 3º tinha usado antes também db.openOrCreateDatabase(...), claro com os parâmetros de entrada de forma correta! But for some reason I wasn’t solving my problem! SOLUÇÃO: A solução para o meu problema está em Context, ready solved my problem!!

1

The main reason for creating this class for creating and manipulating databases is reuse it and what’s more this class didn’t need to extend any other!

Below My Class after solving the problem.
BancoDeDados.java

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

 public class BancoDeDados {
 private String DATABASE_NAME;
 private int DATABASE_VERSION;
 private String TABLE_NAME;
 private String SQL_SELECT_ALL;
 private String SQL_SELECT_ID;
 private String SQL_CREATE;

 public BancoDeDados(String DATABASE_NAME,int DATABASE_VERSION,String TABLE_NAME,String SQL_SELECT_ALL,String SQL_SELECT_ID,String SQL_CREATE){
     this.DATABASE_NAME = DATABASE_NAME;
     this.DATABASE_VERSION = DATABASE_VERSION;
     this.TABLE_NAME = TABLE_NAME;
     this.SQL_SELECT_ALL = SQL_SELECT_ALL;
     this.SQL_SELECT_ID = SQL_SELECT_ID;
     this.SQL_CREATE = SQL_CREATE;
 }
 public BancoDeDados(String DATABASE_NAME,int DATABASE_VERSION,String TABLE_NAME,String SQL_SELECT_ALL,String SQL_CREATE){
     this.DATABASE_NAME = DATABASE_NAME;
     this.DATABASE_VERSION = DATABASE_VERSION;
     this.TABLE_NAME = TABLE_NAME;
     this.SQL_SELECT_ALL = SQL_SELECT_ALL;
     this.SQL_CREATE = SQL_CREATE;
 }

public void onCreate(Context ctx,SQLiteDatabase db){
     //openOrCreateDatabase --> Cria ou Abre banco de dados
     //(nome.db,permissão (modo), ...)
     // MODE_PRIVATE --> Priva o acesso do banco para somente aplicação
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE, null);
     db.execSQL(this.SQL_CREATE); //Criando tabela caso não exista!!
     db.close();
 }

public void onUpgrade(Context ctx,SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + this.TABLE_NAME);

    // Create tables again
    onCreate(ctx,db);
 }

 public long onWrite(Context ctx,SQLiteDatabase db,String row,ContentValues ctv){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     long lg = db.insert(this.TABLE_NAME,row,ctv);
     db.close();
     return lg;
 }

 public int onUpdate(Context ctx,SQLiteDatabase db,ContentValues ctv,String row,int id){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     int x = db.update(this.TABLE_NAME, ctv, row, new String[]{String.valueOf(id)});
     db.close();
     return x;
 }

 public Cursor onSelecAll(Context ctx,SQLiteDatabase db){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     Cursor cursor = db.rawQuery(this.SQL_SELECT_ALL, null);
     return cursor;
 }

 public Cursor onSelecId(Context ctx,SQLiteDatabase db,int id){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     Cursor cursor = db.rawQuery(SQL_SELECT_ID, new String[]{String.valueOf(id)});
     return cursor;
 }

 public int onDelete(Context ctx,SQLiteDatabase db,String row,int id){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     int x = db.delete(this.TABLE_NAME, row, new String[]{String.valueOf(id)});
     db.close();
     return x;
 }

 public void onClose(Context ctx,SQLiteDatabase db){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     db.close();
 }

}

And in the Main.java we have:

     import android...
     public class Main extends Activity {

 private SQLiteDatabase db;
 private Context ctx;
 private static final int DATABASE_VERSION = 1;
 private static final String DATABASE_NAME = "bancodedado.db";
 private static final String TABLE_NAME = "tabela";
 private static final String SQL_SELECT_ALL = "SELECT * FROM "+TABLE_NAME;
 private static final String SQL_SELECT_ID = "SELECT * FROM tabela WHERE _id = ?";
 /* SQL de criação do banco de dados. */
 private static final String SQL_CREATE = "CREATE TABLE IF NOT EXISTS tabela(" +
                    "_id INTEGER PRIMARY KEY, " +
                    "pessoa VARCHAR(30), " 
                    "animal VARCHAR(30))";
 BancoDeDados BD = new BancoDeDados(DATABASE_NAME,DATABASE_VERSION,TABLE_NAME,SQL_SELECT_ALL,SQL_SELECT_ID,SQL_CREATE);
  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  
    ctx = getBaseContext(); //Context para usar na classe BancoDeDados
    BD.onCreate(ctx, db); // Criar BD e tabela caso sejam necessários!! 
   /* .... */
 }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.