0

I made a big proyect for Android using a prepopulated SQLite Database and it was working perfectly. Now I'm trying to reuse it but It doesn't work on Android 6.0+ Devices, on older android versions it's still working. Problem is everytime I try to access the data, the database is empty. I was trying stuff and I made the most simple app I could think of.

I've been trying to change "/data/data/..../database" for any method to get a proper location but there is no way to make it work. Maybe I need special permisions to make it work in 6.0+ Android or something like that? Any help? Thanks!!

public class MainActivity extends AppCompatActivity {

    private DataBaseHelper dbHelper;

    TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView1 = findViewById(R.id.textViewText);

        databaseConexion();

        textView1.setText(dbHelper.getAll());
    }


    private void databaseConexion()
    {
        dbHelper = new DataBaseHelper(this);
        try {
            dbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");

        }
    }
}
public class DataBaseHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "/data/data/enkend.apps.proyects.pogo.sqlitetest/databases/";

    private static String DB_NAME = "Test.db";

    private SQLiteDatabase myDataBase;

    private final Context myContext;


    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     *
     * @param context
     */
    public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    public void createDataBase() throws IOException {

        myContext.deleteDatabase(DB_NAME);
        boolean dbExist = checkDataBase();

        if (!dbExist) {

            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }
    }

    private boolean checkDataBase() {

        File databasePath = myContext.getDatabasePath(DB_NAME);
        return databasePath.exists();
    }


    private void copyDataBase() throws IOException {


        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    @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 String getAll()
    {
       String str= "";

        // Select All Query
        String selectQuery = "SELECT Name FROM Fortnight ORDER BY Name";

        SQLiteDatabase db = this.getReadableDatabase();
        try {

            Cursor cursor = db.rawQuery(selectQuery, null);
            try {

                // looping through all rows and adding to list
                if (cursor.moveToFirst()) {
                    //int i = 0;
                    do {
                        return cursor.getString(0);
                    } while (cursor.moveToNext());

                }

            } finally {
                try { cursor.close(); } catch (Exception ignore) {}
            }

        } finally {
            try { db.close(); } catch (Exception ignore) {}
        }
        return str;
    }
Enkend
  • 1
  • 3

1 Answers1

0

You are creating a new empty database with this.getReadableDatabase(); and then trying to override it with copyDataBase();? I think it should be the other way around.

georgij
  • 2,054
  • 4
  • 27
  • 43