Query database from internal memory

0

I am working on an app and I need to use a BD SQlite (already created) from the internal memory of the mobile. I have searched and can not find, I already know how to create the BD from scratch in the app itself or copy a BD to the assets folder and work with it, but I can not find anything about how to use the BD already created from the memory of the phone ... or application on the one hand and BD on /scard/AplicacionX/basedatosx.bd /

    
asked by Felix A Marrero Pentón 21.03.2017 в 08:32
source

1 answer

0

with this class I solved the problem

public class MiBaseDatos extends SQLiteOpenHelper {

    //Ruta por defecto de las bases de datos en el sistema Android
    private static String DB_PATH = "/FelixDroyd/";
    private static String DB_NAME = "etecsa.db";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    //contructor con referencia a la memoria interna del movil

   // Environment.getExternalStorageDirectory().getPath() hace referencia a /storage/emulated/0
   //    "/storage/external_SD" este e pa la externa

    public MiBaseDatos(Context context) {
        super(context, "/storage/external_SD" +DB_PATH + DB_NAME, null, 1);
        this.myContext = context;

    }
    /**
     * Comprueba si la base de datos existe
     *
     * @return true si existe, false si no existe
     */
   public boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {

            String myPath = "/storage/external_SD" +DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
//si llegamos aqui es porque la base de datos no existe todavía.
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    @Override
    public synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public Persona recuperarPERSONA(String numero) {
        SQLiteDatabase db = getReadableDatabase();
        String[] valores_recuperar = {"number","name","address","identification","province"};
        Cursor c = db.query("movil", valores_recuperar, "number=" +"'"+ numero+"'",
                null, null, null, null, null);
        Persona cuerpo = null;
        if (c != null && c.moveToFirst()) {

           cuerpo = new Persona(c.getString(0), c.getString(1), c.getString(2), c.getString(3), c.getInt(4));
            db.close();
            c.close();
        }else{
          cuerpo = new Persona(" "," ", " ", " ", 1);
        }

        return cuerpo;
    }
}

and with the method recoverPERSON () ago the consultations ....

    
answered by 22.03.2017 в 19:50