How do I insert previous tuples into tables in a Sqlite3 database?

Asked

Viewed 38 times

0

I’m developing an RPG-style app using Android Studio. This app needs a database and as there is no need to be online, I am using Sqlite 3. The problem is that some data need to be previously entered in the BD, as configuration data of the app and some characters in the game, for example. Reading the documentation for Android developers, I saw that the onUpgrade() method should be used only for changes in the database structure (SQL-DDL), not including the insertion of tuples in the tables (SQL-DML). Is it possible to insert previous tuples into some tables without having to do it through Java code in some class of my app? For example, via a DBMS or command prompt? I ask this, because a BD Sqlite of an app on Android is located in an internal folder of the app itself and, the most I can do is download a copy of the BD file. However, because it is a copy, any changes made to it do not reflect in the original file inside the app. Below follows the class that allows creating and updating the BD:


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

import androidx.annotation.Nullable;

public class DadosOpenHelper extends SQLiteOpenHelper {

    private static final String NOME_BD = "BD_RPG_CL_PROJECT.db";
    private static int VERSAO_BD = 1;

    //Construtor
    public DadosOpenHelper(@Nullable Context context) {
        super(context, NOME_BD, null, VERSAO_BD);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        /*A primeira vez que o método getWritableDataBase() for chamado,
        o onCreate, onUpgrade() e o onOpen(), esse último de implementação opcional,
        serão chamados*/
        db.execSQL(ScriptDDL.getCreateTableTbPersonagemJog());
        db.execSQL(ScriptDDL.getCreateTableTbCategoriaTipo());
        db.execSQL(ScriptDDL.getCreateTableTbCenario());
        db.execSQL(ScriptDDL.getCreateTableTbNarrador());
        db.execSQL(ScriptDDL.getCreateTableTbTipoCaract());
        db.execSQL(ScriptDDL.getCreateTableTbCaract());
        db.execSQL(ScriptDDL.getCreateTableTbAventura());
        db.execSQL(ScriptDDL.getCreateTableTbAbertura());
        db.execSQL(ScriptDDL.getCreateTableTbJogo());
        db.execSQL(ScriptDDL.getCreateTableTbConfig());
        db.execSQL(ScriptDDL.getCreateTableTbOrdemJogo());
        db.execSQL(ScriptDDL.getCreateTableTbTrecho());
        db.execSQL(ScriptDDL.getCreateTableTbEvento());
        db.execSQL(ScriptDDL.getCreateTableTbAcao());
        db.execSQL(ScriptDDL.getCreateTableTbCaractAcao());
        db.execSQL(ScriptDDL.getCreateTableTbCaractEvento());
        db.execSQL(ScriptDDL.getCreateTableTbMomento());
        db.execSQL(ScriptDDL.getCreateTableTbConfigApp());

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Método para realizar qualquer atualização no BD, como incluir um campo numa tabela, por exemplo

    }

    public SQLiteDatabase estabeleceConexao() {
        return this.getWritableDatabase();
    }

    public void fechaConexao(){
        this.getWritableDatabase().close();
    }
}
No answers

Browser other questions tagged

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