Creating more than one Android Studio table (Sqlite)

Asked

Viewed 4,016 times

1

I’m trying to develop a mobile app for a final job of my college, it’s pretty simple!

The application has to save the information registered in the database.

However I would have to create 4 different tables to store the data, the tables would be: Usuário, Aplicativo, Empresa and Funcionários.

And after registering perform Selects that the teacher wants.

The Problem: I don’t know if it is possible to make this creation of 4 different tables on Android Studio and then insert data into them, to make the selects.

I’ve done an App similar to this before, but only used 1 table.

  • The same process you use to create a table, just repeat and create all four. In Sqlite onCreate do not forget to give the CREATE table 4x

  • Yes but I will be able to enter the data and make the selects of these 4 tables ?

  • Of course, a database can have (virtually) infinite tables. You just need to make the methods agnostic as to which table are working (passing parameters) or do a method for each table for each operation.

2 answers

2


If understood, you want to create the table and already insert the values?

You can perform as many operations as needed on onCreate.

Follow an example:

@Override
public void onCreate(final SQLiteDatabase db) {

    //Vamos criar as tabelas... 
    db.execSQL("CREATE TABLE Usuario ( id INTEGER PRIMARY KEY, nome TEXT )");
    db.execSQL("CREATE TABLE Aplicativo ( id INTEGER PRIMARY KEY, nome TEXT )");
    //Seus demais INSERTS...

    /**
     * Agora vamos inserir os dados na tabela...
     **/

    db.execSQL("INSERT INTO Usuario values (1, 'Usuario Um')");
    db.execSQL("INSERT INTO Usuario values (2, 'Usuario Dois')");
    db.execSQL("INSERT INTO Usuario values (3, 'Usuario Tres')");
    db.execSQL("INSERT INTO Usuario values (4, 'Usuario Qautro')");
}

1

Friend, using database to persist data is very common, and for this has some alternatives.

  1. You can build an API, which you query, and enter data. Using a server, with the language you want. Communicating via JSON.
  2. If you don’t have a certain need for communication between other devices, and want to persist this data locally on the device you can use some platform. I personally like the DB Flow or even implement "at hand" with the library SQLiteOpenHelper. DEVMEDIA Tutorial

Browser other questions tagged

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