Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript SQLite数据库在Android 4.4中不工作_Javascript_Android_Sqlite_Cordova - Fatal编程技术网

Javascript SQLite数据库在Android 4.4中不工作

Javascript SQLite数据库在Android 4.4中不工作,javascript,android,sqlite,cordova,Javascript,Android,Sqlite,Cordova,我在PhoneGap项目中使用SQLite数据库。除了Android 4.4.0+,我测试过的所有其他操作系统都会填充该数据库 访问数据库的代码如下:- public class MathWhiz extends CordovaActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supe

我在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);
有人能指出我需要做什么更新才能在安卓4.4+上运行吗?谢谢你改变这个

    this.copy("Databases.db", "/data/data/" + pName + "/databases/");
    this.copy("sample.db", "/data/data/" + pName + "/databases/");

试试看。。它工作得很好

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;
    }

我不知道phonegap的事。但在Android数据库中,路径应该是data/data//databases/。您应该将数据库复制到此文件夹。@Zohrakan pName仅表示包名。即使我的意思相同,也同意您的观点。在我的评论中,我想指出这一行。copy(“sample.db”,“/data/data/”+pName+“/app_database/myFile/”)@zohrakan:是的,我想知道为什么它不工作。问题是它没有像4.4中那样创建表。它是在复制数据库吗?您可以通过sqlite浏览器或终端进行检查。复制的数据库的值是多少?放置一些日志消息以进行调试。还有一件事就是删除你的数据库;亚行壳牌;cd数据/数据//数据库/;rm*:很抱歉迟到了,我通过添加SQLLite插件使其在android 4.4上工作。@sumodh您能详细说明SQLLite插件是如何帮助您解决问题的吗。我面临着类似的情况。