【问题标题】:SQlite not working on android 4.4 (kitkat) with phonegapSQlite 无法在带有 phonegap 的 android 4.4 (kitkat) 上运行
【发布时间】:2014-07-20 03:09:49
【问题描述】:

我正在做一个关于字典的APP。

一直运行良好,直到 Android 4.3 (jellybean)

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

所以,我认为复制到数据库文件时有些磨损。


1) 在 onCreate() 中

我将复制到数据库文件资产到数据库位置 (保存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.4)

1) 更改数据库路径:像这样,path = "/data/data/"+getPackageName()+"/app_webview/databases/"; 要么 path = "/data/data/"+getPackageName()+"/databases/";

2) 升级 cordova 3.5(phonegap) 并添加 SQLite 插件 (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 中不起作用 ....

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

但是,,, 我需要使用 Database ONLY SELECT 函数。

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

很简单..

所以,请任何人解决 sqlite 问题的方式或建议其他方式...

或者我错过了什么?

我三天都睡不着觉……关于这个问题。 T.T

谢谢,

【问题讨论】:

  • 欢迎堆栈溢出。感谢您提出足够详细的问题。我想说的好问题!
  • 这个问题你解决了吗?
  • 你是怎么解决这个问题的?

标签: android database sqlite cordova android-4.4-kitkat


【解决方案1】:

我做了类似的事情

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

所以在版本

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多