javascript - SQLite 数据库在 Android 4.4 中不工作

标签 javascript android sqlite cordova

我正在为我的 PhoneGap 项目使用 SQLite 数据库。除了 Android 4.4.0+ 之外,我测试过的所有其他操作系统都在填充数据库。

访问数据库的代码如下:-

public class MathWhiz extends CordovaActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        super.loadUrl(Config.getStartUrl());
        SharedPreferences sp = getSharedPreferences("MYPREFS",
                Activity.MODE_PRIVATE);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // If no shared prefs exist, e.g. first install, it doesn't matter - the
        // following will return false as a default
        Boolean database_copied = sp.getBoolean("database_copied", false);    
        if (!database_copied) {
            try {
                String pName = this.getClass().getPackage().getName();

                this.copy("Databases.db", "/data/data/" + pName
                        + "/app_database/");
                this.copy("sample.db", "/data/data/" + pName
                        + "/app_database/myFile/");
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("database_copied", true);
                editor.apply();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    void copy(String file, String folder) throws IOException {

        File CheckDirectory;
        CheckDirectory = new File(folder);
        if (!CheckDirectory.exists()) {
            CheckDirectory.mkdir();
        }
        InputStream in = getApplicationContext().getAssets().open(file);
        OutputStream out = new FileOutputStream(folder + file);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0)
            out.write(buf, 0, len);
        in.close();
        out.close();

    }

}

这就是我使用数据库的方式:-

window.openDatabase("sampleDB", "1.0", "sample", 200000);

任何人都可以指出我需要做哪些更新才能使其在 Android 4.4 + 上运行吗?谢谢

最佳答案

试一试..它运作良好...

public boolean copyDataBaseFromAssets(Context c) throws IOException {

    if(android.os.Build.VERSION.SDK_INT >= 17)
            DB_PATH = context.getApplicationInfo().dataDir + "/databases/"+ DATABASE_NAME;
        else
            DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"+DATABASE_NAME;

        String pathToDatabaseFileInAssetFolder = DATABASE_NAME;
        String pathToDatabaseFileInSystem = DB_PATH;


this.getReadableDatabase(); 

> getReadableDatabase 函数用于数据库导入代码

        AssetManager assetManager = c.getResources().getAssets();
        InputStream inputStream = null;

        try {

            inputStream = assetManager.open(pathToDatabaseFileInAssetFolder);
        } catch (IOException ex) {

            return false;
        }

        if (inputStream != null) {

            OutputStream outputStream = new FileOutputStream(pathToDatabaseFileInSystem);

            byte[] buffer = new byte[1024];
            int length;

            while ((length = inputStream.read(buffer)) > 0) {

                outputStream.write(buffer, 0, length);
            }

            outputStream.flush();
            outputStream.close();
            inputStream.close();

            Log.d(TAG, "Database is copied");
            return true;
        }

        return false;
    }

关于javascript - SQLite 数据库在 Android 4.4 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21407444/

相关文章:

javascript - 清除 SELECT 中的所有选择

php - 每天更换一个DIV之间的内容

android - CameraKit 未按预期运行 v1.0.0 beta 3.11。单击按钮不拍照

android - 在没有预定义结构关系的情况下在 Realm 数据库上实现一种形式的连接查询

SQLiteObject 在 ionic 3 中未定义

javascript - websockets 是否允许 p2p(浏览器到浏览器)通信?

android - 有没有办法在不实际显示的情况下启动 Chrome 自定义标签?

android - 如何设置添加 child 时调用的onChildAdded

c - 使用 SQLITE_OPEN_CREATE 打开数据库时设置组可写

javascript - 在 Jquery 倒计时器中用当前日期时间替换硬编码日期时间