首页 文章

SQlite没有使用带有phonegap的android 4.4(kitkat)

提问于
浏览
1

我正在制作一本关于字典的APP .

它很好,直到Android 4.3(软糖)

但在Android 4.4上,APP maikng有关数据库的错误 . (找不到表)

所以,我认为复制到DB文件有些麻烦 .


1)in onCreate()

我将db文件资产复制到db locaion(保存14个拆分文件)

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)关于HTML:

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

我正在尝试(为了Android 4)

1)更改了数据库路径:像这样,path =“/ data / data /”getPackageName()“/ app_webview / databases /”; OR path =“/ data / data /”getPackageName()“/ databases /”;

2)升级cordova 3.5(phonegap)并添加SQLite pulgin(https://github.com/brodysoft/Cordova-SQLitePlugin

  • 测试很好,但他们在Android 4.4(T.T)中说问题

3)设置为webwiew(如下https://stackoverflow.com/questions/22568697/webview-created-database-use-it-in-android-4-4

  • 但是,4.4不工作....

所以......最后,我不能在Android 4.4中使用带有Phonegap 3.5的SQLite ......对吗?

但是,,,我需要使用数据库ONLY SELECT功能 .

这个APP只是加载数据并在屏幕上查看 .

它很简单..

所以请,任何人修复sqlite问题的方式或建议另一种方式...

或者我错过了什么?

我无法入睡3天..关于这个问题 . T.T

谢谢,

1 回答

  • 0

    我做了类似的事情

    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
    

    因此,在版本<4.4中,它是app_database文件夹,对于4.4,它是app_webview文件夹 . 以上对我有用 .

相关问题