Open database Android

349
April 22, 2017, at 03:01 AM

I need to open a database from my phone's memory (/storage/emulated/0) and I've got "SQLiteCantOpenDatabaseException" the database to open (created by DB browser from linux) have the tables "android_metadata" and them respective fields (locale...es_US).

I need open and read data from the external db (the app need to download the db from website and read data and put in textview into the app activities )

The app has permission to read and write external memory

code below (only the code of button to get the database reference)

case R.id.button4:
            {
                int ID,IDn,IDa;
                String cabeceras[] ={"nombre","apellido",_ID},lectura=new String(),err="";
                ArrayList<String> arregloDatos = new ArrayList<>();
                try {
                    SQLiteDatabase db = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory().getPath() +"/"+"pruebaDB.db", null, SQLiteDatabase.OPEN_READWRITE);
                    Cursor cursor = db.query("persona", cabeceras, null, null, null, null, null);

                    ID = cursor.getColumnIndex(_ID);
                    IDn = cursor.getColumnIndex("nombre");
                    IDa = cursor.getColumnIndex("apellido");
                    while (cursor.moveToNext()) {
                        lectura = cursor.getString(ID) + " " + cursor.getString(IDn) + " " + cursor.getString(IDa);
                        arregloDatos.add(lectura);
                    }
                    Intent i = new Intent(this, Main2Activity.class);
                    i.putStringArrayListExtra("cadena", arregloDatos);
                    startActivity(i);
                }catch (Exception g)
                    {
                        Toast msg =Toast.makeText(this,g.getClass().toString(),Toast.LENGTH_SHORT);
                        msg.show();
                    }
                break;
            }
Answer 1

I don't think you are able to write SQLDatabases to external. If it is all string or int data, you could write the Database to internal, then write a function that will iterate through the table and concatenate the data and write it to external as a .txt.

Yep, just read the documentation on the .openDatabase method. I think the first param is looking for the path portion of the content URI, not an ext memory file path.

https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#openDatabase(java.lang.String,%20android.database.sqlite.SQLiteDatabase.CursorFactory,%20int,%20android.database.DatabaseErrorHandler)

READ ALSO
Updating TableLayout from another class

Updating TableLayout from another class

I am really new to android and I have been reading on stackeroverflow, that we need to pass the Context when updating TableLayout from another classBut I am not trying to update the TableLayout directly from another class; i am trying to update it by calling...

197
Android - Simple 3d Drawing

Android - Simple 3d Drawing

I wanted to make a 3d representation of a tube based on x,y,z coordinates and a radius to display a 3d tube on the smartphoneWould be good also to be able to rotate the camera

232
cordova local notification click event parameters are empty

cordova local notification click event parameters are empty

I'm working on a cordova application which has a local notification pluginwhich I found at this git: https://github

306
How modify Func1 in to Function in RxJava 2.0 Android

How modify Func1 in to Function in RxJava 2.0 Android

This is my code i don't understand how to transform Funciont instead Func1 please help me!!! Thanks!!

392