Why does that line of code make me wrong?

Asked

Viewed 129 times

-1

This line of code gives me error in sentence SQLiteDatabase.CREATE_IF_NECESSARY and stops me from making the database. Because this is happening?

  SQLiteDatabase BaseDados=SQLiteDatabase.openOrCreateDatabase("BaseDados",SQLiteDatabase.CREATE_IF_NECESSARY,null);

The mistake is this:

Error:(16, 54) error: no suitable method found for openOrCreateDatabase(String,int,) method Sqlitedatabase.openOrCreateDatabase(String,Cursorfactory,Databaseerrorhandler) is not applicable (actual argument int cannot be converted to Cursorfactory by method Invocation Conversion) method Sqlitedatabase.openOrCreateDatabase(String,Cursorfactory) is not applicable (current and formal argument lists differ in length) method Sqlitedatabase.openOrCreateDatabase(File,Cursorfactory) is not applicable (current and formal argument lists differ in length)

The code I made is this::

public class GestorBaseDados {
Context contexto;
SQLiteDatabase BD;
public GestorBaseDados(Context cont) {
    contexto=cont;
    String erro=null;
    try {

  BD=SQLiteDatabase.openOrCreateDatabase("BaseDados",SQLiteDatabase.CREATE_IF_NECESSARY,null);


    }catch (Exception e)
    {
        Toast.makeText(contexto,"erro "+e.toString(),Toast.LENGTH_LONG).show();

    }



}

}

This is a class that will have all the methods to mess with the database.

  • 2

    Puts the error that occurred

  • 1

    And what error does it make? Without the error message becomes more difficult.

  • Was this : Error:(16, 54) error: no suitable method found for openOrCreateDatabase(String,int,<null>) method Sqlitedatabase.openOrCreateDatabase(String,Cursorfactory,Databaseerrorhandler) is not applicable (actual argument int cannot be converted to Cursorfactory by method Invocation Conversion) method Sqlitedatabase.openOrCreateDatabase(String,Cursorfactory) is not applicable (actual and formal argument lists differ in length) method Sqlitedatabase.openOrCreateDatabase(File,Cursorfactory) is not applicable (actual and formal argument lists differ in length)

  • won’t even let me compile, the project

1 answer

2

There is no Overload for the method SQLiteDatabase.openOrCreateDatabase() with that signature.

Use

SQLiteDatabase.openOrCreateDatabase("BaseDados", null);

which is the equivalent of

SQLiteDatabase.openDatabase("BaseDados", null, SQLiteDatabase.CREATE_IF_NECESSARY). 
  • the openDatabase method creates the database as well?

  • Create if necessary, see that the flag is passed SQLiteDatabase.CREATE_IF_NECESSARY

  • both the first method you have indicated to me and the second method make me mistake

  • Same error? See if you are typing the call to the method correctly.

  • is no longer the same is another for the first option you have indicated I will pass you the code

  • You have not changed the code. Replace BD=SQLiteDatabase.openOrCreateDatabase("BaseDados",SQLiteDatabase.CREATE_IF_NECESSARY,null); for BD=SQLiteDatabase.openOrCreateDatabase("BaseDados",null);

  • I already did that, only when I put the code on the site, I switched it to the original form for other people to see how it was. And it still doesn’t work, I made a Toast and it tells me that it can’t make the database.

  • I don’t know exactly what you want to do but the normal thing is to use a class derived from Sqliteopenhelper instead of using the class Sqlitedatabase. Behold here an example of implementation.

  • already solved the problem if you have to add a this before the method gets like this: BD=this.openOrCreateDatabase("Basedata",CREATE_IF_NECESSARY,null);

Show 4 more comments

Browser other questions tagged

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