Accessing SQLite database in another activity

1

I'm starting out in java android, I'm quite lay still, I'm catching the POO concept little by little.

My problem is as follows, I have a class BankBank and another BankController where I have some methods to manipulate the bank, this in an activity with name menu, so far everything works.

The question is whether I need to manipulate the bank's data in another activity, which is the correct way to do this.

At first I thought about repeating the class in the other activity but I was afraid to do this: the bank was going to be rewritten and I also found it inelegant to repeat the whole code. So I thought I did not have a way to instantiate this class in the other activity.

Below the code I used to create and manipulate the database, just the basics, I removed the methods to not be extensive.

public class BancoController {


    private SQLiteDatabase db;
    private CriaBanco banco;

    public BancoController(Context context) {

        banco = new CriaBanco(context);
    }



}

public class CriaBanco extends SQLiteOpenHelper {

    private static final int VERSION = 1;
    private static final String DB_NAME = "dbcoletor.db";

    public CriaBanco(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    public void onCreate(SQLiteDatabase db) {


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i2) {
        //--nothing for now--
    }
}

To solve I created a class in the other activity and I used this form:

private SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("data/data/com.coletorrc/databases/dbcoletor.db", null);

So I was able to manipulate the bank, although it worked, I was wondering if there would be a more elegant way to do it.

    
asked by anonymous 22.06.2017 / 22:20

2 answers

1

You can do it this way:

// Criar os objetos do banco na sua Activity
CriaBanco helper = new CriaBanco(this);
SQLiteDatabase db = helper.getWritableDatabase();

...

// Manipular o banco com query, insert, delete ou update     
Cursor cursor = db.query(...);
...
cursor.close();

...

db.insert(...);
db.delete(...);
db.update(...);

...

// Fechar a conexão com o banco quando terminar o uso
db.close();

If you want even more elegance, take a look at ContentProviders, rs:

link

    
22.06.2017 / 22:27
1

It is not a rule, but the usual and the simplest is to do what you did: a class inherited from SQLiteOpenHelper and possibly another with methods to access / manipulate the data in the database. These classes should not be declared in Activity but rather in separate java files. Thus, you can easily get an instance of them in any activity / class.

Normally both are singleton and only the second one uses the first.

All access and manipulation of data in the database is done using the second.

Of course you can do without the second by passing your methods to the first one.

The most important thing here is to have only one instance of SQLiteOpenHelper whose main responsibility is to manage the creation / version of the database and to make available an object of type SQLiteDatabase, using methods getWritableDatabase() and / or getReadableDatabase() . >

See here for an example.

    
22.06.2017 / 22:55