How can I rename a database on android?

1

What happens is that at the beginning The database name without the extension ".db" and when reviewing data does not appear but if I create a new one with the extension ".db" if it appears, this is uncomfortable because I can not see what is happening in the db. Does anyone know how to rename the database in sqlite of android?

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "BDPersons";

    String tablaPerson = "create table person(" +
            "id varchar(100) primary key," +
            "nombre varchar(30))";


    public BaseDatos(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(tablaPerson);
    }
    
asked by goku venz 27.03.2018 в 16:03
source

2 answers

0

You will have to create a new file, copy the old file from the old DB and then delete the old DB. This would be done by creating a file with the extension you want, rename the new db to the name of the new one using File#renameTo and then delete the old one:

public void renombrarDbSiRequerido(String nombreArchivoViejaDb, String nuevoArchivoDb)
{
    File viejaDb = new File(nombreArchivoVieajDb);
    File nuevaDb = new File(nuevoArchivoDb);

     // verificamos ya fue creado el nuevo archivo para db
    if(!nuevaDb .exists())
    {
       // como no existe, procedemos a crear el nuevo archivo de la db
       nuevaDb.createFile();

       // renombramos la vieja por el nuevo
       viejaDb.renameTo(nuevaDb);
    }
}

Where the parameter nombreArchivoViejaDb is the full path of the db that you want to rename and the parameter nuevoArchivoDb is the full path of the new name;

    
answered by 27.03.2018 в 16:29
0

If you check your class that extends from SQLiteOpenHelper you can see the variable DATABASE_NAME where you can rename its value by adding the extension:

 private static final String DATABASE_NAME = "BDPersons.db";

This value of the variable can be renamed and added the extension, in this way your application will continue working without problem and you will be able to see your file identified as a database since it was created with the extension ".db".

   private static final int DATABASE_VERSION = 1;
   private static final String DATABASE_NAME = "BDPersons.db"; //*

    String tablaPerson = "create table person(" +
            "id varchar(100) primary key," +
            "nombre varchar(30))";


    public BaseDatos(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(tablaPerson);
    }

It is important to remove the application cache, delete the application or change the value of DATABASE_VERSION to see the change reflected in your Database.

    
answered by 27.03.2018 в 17:34