0

friends.

I try to use a pre-created custom database in my android app. First I copy my DB using this code.

public void onCreate(SQLiteDatabase db) {



    final String DB_DESTINATION = "/data/data/com.example.android.sqlitedatabaseexample/databases/turkish2.db";


            boolean initialiseDatabase = (new File(DB_DESTINATION)).exists();
            if (initialiseDatabase == false) {


                // Open the .db file in your assets directory
                InputStream is = null;
                try {

                    is = context.getAssets().open("turkish2.db");


                // Copy the database into the destination
                OutputStream os = new FileOutputStream(DB_DESTINATION);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = is.read(buffer)) > 0){
                    os.write(buffer, 0, length);
                }
                os.flush();

                os.close();
                is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

then I check if database exists calling this method

 public boolean checkDataBase() {
            /*Toast.makeText(getApplicationContext(),
                    "работаю", Toast.LENGTH_SHORT).show();*/
            SQLiteDatabase checkDB = null;
            try {
                checkDB = SQLiteDatabase.openDatabase("/data/data/com.example.android.sqlitedatabaseexapple/databases/turkish2.db", null,
                        SQLiteDatabase.NO_LOCALIZED_COLLATORS/*OPEN_READONLY*/);
                Toast.makeText(getApplicationContext(),
                        "БД на месте", Toast.LENGTH_SHORT).show();
                checkDB.close();
            } catch (SQLiteException e) {
                // database doesn't exist yet.
                Toast.makeText(getApplicationContext(),
                        "БД не существует", Toast.LENGTH_SHORT).show();
            }
            return checkDB != null;
        }

...it shows that my database exists! Then I try to make query but get an exception.

My database contains two tables - android_metadata and turkish. BUT. While I check all tables using this

Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);

                if (c.moveToFirst()) {
                    while ( !c.isAfterLast() ) {
                        Toast.makeText(this, "Table Name=> "+c.getString(0), Toast.LENGTH_LONG).show();
                        c.moveToNext();
                    }
                    Toast.makeText(this, "DATS ALL SUKA", Toast.LENGTH_LONG).show();
                }

It showl like I have only android_metadata table. Alse when I try to make query to the table I need (turkish table) it shows SQLiteException: no such table 'turkish'

What am I doing wrong? Help please.

Thanks in advance!

2
  • 2
    Consider using sqlite-asset-helper
    – laalto
    Aug 17, 2016 at 19:30
  • Now it works! Thank you so much, friend, u saved my life!
    – Dmytro
    Aug 18, 2016 at 12:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.