0

I have a mySQL script, with my database, I would like import this DB to my android app using that script, when I executed this code It works (I don´t have any exception or error) but when I try to get information from DB It doesn´t work.

    private static String DB_PATH = "/data/data/com.example.importDB/databases/";
    private static String DB_NAME = "mydb.sql";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        if(dbExist){
            //do nothing - database already exist
        }else{
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    private boolean checkDataBase(){
        SQLiteDatabase checkDB = null;
        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }catch(SQLiteException e){
            //database does't exist yet.
        }
        if(checkDB != null){
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException{
        InputStream myInput = myContext.getResources().openRawResource(R.raw.mydb);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }

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

        super.close();
    }

I have my "mydb.sql" in res/raw folder

This is mySql script:

https://mega.nz/#!KUkA0JRY!r2BsQwmJbtmxgTAB1DZVpG-j_JO4oEhY9oIfg3K4Ohk

Thanks

Ruben Manzano
  • 39
  • 3
  • 12
  • your script link is asking for a "decryption key" - also, are you importing a script? Or trying to run a script to import data? A mySQL DB should have more than one file... – Jim Jan 04 '18 at 10:55
  • I update the link, I am trying run the script and generate the DB with all information – Ruben Manzano Jan 04 '18 at 11:10

1 Answers1

0

You are copying a SQL script and then using it as the database. This will not work.

You need to create a new DB in Android. Then you have a couple options to run the script SQL:

1) if it is important to keep it in the file, you could find a tool like ScriptRunner (http://gulvaniharesh.blogspot.com/2013/08/import-sql-script-of-mysql-from-java.html) - I haven't used it but Android doesn't have anything like that.

2) Move your SQL script to static strings that you execute when the DB is created. I have seen this and done this a lot.

3) Move your SQL script to an XML resource and then execute like in #2 above. I have also seen and done this a lot.

Jim
  • 10,172
  • 1
  • 27
  • 36