Access Sqlite database in another Activity

Asked

Viewed 415 times

1

I’m starting in java android, I’m quite layy yet, I’m picking up the concept of POO gradually.

My problem is this, I have a Criabanco class and another Bancocontroller where I have some methods to manipulate the bank, this in an Activity with menu name, up to here everything working.

The doubt and if I need to manipulate the database data in another Activity, what is the correct way to do this.

At first I thought about repeating the class in the other Activity but I was afraid of doing this: the bank was going to be rewritten and I also found it inelegant to repeat all the code.
So I was wondering if there’s a way to instantiate this class in the other Activity.

Below the code I used to create and manipulate the bank, only the basic, I removed the methods not to be extended.

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 used this form:

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

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

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:

https://developer.android.com/guide/topics/providers/content-providers.html

  • Marcio, just that I need this for the other Activity, the Create Bank class in my case is in an Activity, that when I click a button passes to another, in this other one that I need to manipulate the bank, taking advantage...if I instantiate this class Create bank more than once...or then in another Activity, it overwrites the bank created?

  • You can create the Create Database class separately (create a file of your own, to be more organized) and instantiate it where necessary. The seat shall not be overwritten.

  • I managed to do as you instructed.

1

It is not a rule, but the usual and the most "simple" is to do what you did: a class inherited from Sqliteopenhelper and eventually another with methods to access/manipulate the data in the database.
These classes should not be declared in Activity but in separate java files. So you can easily get an instance of them in any Activity/class.

Usually, both are Singleton and only the second uses the first.

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

Of course you can dispense with the second by passing your methods to the first.

The most important thing here is to have only one instance of Sqliteopenhelper, whose main responsibility is to manage the creation/version of the bank and make available an object of type Sqlitedatabase, through the methods getWritableDatabase() and/or getReadableDatabase().

Behold here an example.

  • Sorry to "Leiguice", in the main Activity, I have the class that manipulates the bank, with the methods I need, I want to know if it is possible to use these methods in another Activity, in the case in the class that controls the bank I have a way to insert, I wanted to know if there is a way to use this method in another Activity without having to re-write it, currently my code is working, I’m just trying to improve it.

  • These classes should not be "written" in Activity but in separate java files. See the answer edit.

  • Thanks ramaral, I created a separate java file and got what I needed.

Browser other questions tagged

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