1
votes

I'm making a APP about dictionary.

it was woring well, until Android 4.3 (jellybean)

but on Android 4.4, APP maikng error about database. (can not found table)

so, I think something worng in copy to DB file.


1) in onCreate()

i'm copy to db file assets to db locaion (save the 14 split files)

private void CopyInitialDB() {
    String path = ""; 
    if(android.os.Build.VERSION.SDK_INT >= 17)
    {
       path = "/data/data/"+getPackageName()+"/app_webview/databases/";
    }
    else
    {
        path = "/data/data/"+getPackageName()+"/app_database/databases/";
    }
    File topPath = new File(path);
    if ( !topPath.exists() ){
        try {
            topPath.mkdir();
            // try access asset files
            AssetManager assetManager = getAssets();
            // copy Databases.db
            File dbf = new File(path+"Databases.db");
            if (dbf.exists()) {
                dbf.delete();
            }
            InputStream in = null;
            OutputStream out = null;
            try {
                in = assetManager.open("Databases.db");
                out = new FileOutputStream(dbf);
                copyFile(in, out);
            } catch(Exception e) {
                Log.e("tag", e.getMessage());
            } finally {
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            }
            // create folder file__0
            File dbPath = new File(path+"file__0");
            if ( !dbPath.exists() ){
                try {
                    dbPath.mkdir();
                } catch(Exception e) {
                    Log.e("tag", e.getMessage());
                }
            }
            // copy db files
            InputStream[] arrIs = new InputStream[14];
            BufferedInputStream[] arrBis = new BufferedInputStream[14];

            FileOutputStream fos = null;    
            BufferedOutputStream bos = null;

            try
            {
                File f = new File(path+"file__0/0000000000000001.db");

                // 
                if(f.exists())
                {
                    f.delete();
                    f.createNewFile();
                }

                for(int i = 0; i < arrIs.length; i++)
                {
                    arrIs[i] = assetManager.open("000000000000000" + (i+1) + ".db");
                    arrBis[i] = new BufferedInputStream(arrIs[i]);
                }

                fos = new FileOutputStream(f);
                bos = new BufferedOutputStream(fos);

                int read = -1;
                byte[] buffer = new byte[1024];

                for(int i = 0; i < arrIs.length; i++)
                {
                    while((read = arrBis[i].read(buffer, 0, 1024)) != -1)
                    {
                        bos.write(buffer, 0, read);
                    }

                    bos.flush();
                }
            } catch(Exception e) {
                Log.e("tag", e.getMessage());
            } finally {
                for(int i = 0; i < arrIs.length; i++) {
                    try{if(arrIs[i] != null) arrIs[i].close();}catch(Exception e){}
                    try{if(arrBis[i] != null) arrBis[i].close();}catch(Exception e){}
                }

                try{if(fos != null) fos.close();}catch(Exception e){}
                try{if(bos != null) bos.close();}catch(Exception e){}

                arrIs = null;
                arrBis = null;
            }
        } catch(Exception e) {
            Log.e("tag", e.getMessage());
        }
    }
}

2) on HTML :

var db;
var shortName = 'WebSqlDB';
var version = '1.0';
var displayName = 'WebSqlDB';
var maxSize = 20*1024*1024;    
db = openDatabase(shortName, version, displayName,maxSize);

I'M TRYING TO (FOR ANDROID 4.4)

1) changed db path : like this, path = "/data/data/"+getPackageName()+"/app_webview/databases/"; OR path = "/data/data/"+getPackageName()+"/databases/";

2) Upgrade cordova 3.5(phonegap) AND add SQLite pulgin (https://github.com/brodysoft/Cordova-SQLitePlugin)

--> test is well, but they saying problum in Android 4.4 (T.T)

3) setting to webwiew (follows https://stackguides.com/questions/22568697/webview-created-database-use-it-in-android-4-4)

--> but, is not working in 4.4 ....

SO.... finally,, I cant used SQLite in Android 4.4 with Phonegap 3.5... right?

BUT,,, I need Using Database ONLY SELECT function.

this APP is just load data and viewing on screen.

its very simple..

so please, anybody fixed sqlite problem ways or suggest to another ways...

Or anything Am I missing?

I'm can't sleep for 3 days.. about this problem. T.T

thanks,

1
Welcome on stack overflow. Thanks for asking question with enough details. Good question I would say!Paresh Mayani
did you solve this issue?Parth Doshi
how did you solve this issue?Parth Doshi

1 Answers

0
votes

I did something like

String dbfullname = "/data/data/" + getPackageName + "/app_database/file__0/0000000000000001.db";
File f = new File(dbfullname);        
if(f.exists() == false){
    dbfullname = "/data/data/" + getPackageName + "/app_webview/databases/file__0/1";
}
//rest of code

So in versions < 4.4 it's the app_database folder and for 4.4 it's the app_webview folder. The above works for me.