Android 用于PhoneGap应用程序的WebSQL

Android 用于PhoneGap应用程序的WebSQL,android,database,cordova,web-sql,Android,Database,Cordova,Web Sql,我希望使用WebSQL在Android PhoneGap应用程序中存储数据,但不知从何开始。由于应用程序处于脱机状态,因此不需要从web检索数据,我希望有一个可以查询的预填充数据库。有几个问题: 如何创建预填充的数据库?(应该是什么样的文件格式) 如果有两个因素可以更改,那么如何使用SQL从数据库中获得这些因素组合的结果。 如果用户在安卓设备上从设置菜单中清除应用程序数据,数据库会被删除吗? 谢谢你的帮助。感谢您在PhoneGap应用程序中预填充SQLite数据库 1) 您可以借助等工具为应用程

我希望使用WebSQL在Android PhoneGap应用程序中存储数据,但不知从何开始。由于应用程序处于脱机状态,因此不需要从web检索数据,我希望有一个可以查询的预填充数据库。有几个问题: 如何创建预填充的数据库?(应该是什么样的文件格式) 如果有两个因素可以更改,那么如何使用SQL从数据库中获得这些因素组合的结果。 如果用户在安卓设备上从设置菜单中清除应用程序数据,数据库会被删除吗? 谢谢你的帮助。感谢您在PhoneGap应用程序中预填充SQLite数据库 1) 您可以借助等工具为应用程序创建基本数据库,或者如果您的应用程序中已经有数据库,则可以直接获取数据库文件

2) 然后需要通过CLI安装Cordova/PhoneGap SQLitePlugin。()

3) 在html中添加以下脚本文件

<script src="plugins/com.brodysoft.sqlitePlugin/www/SQLitePlugin.js"></script>
5) 对于设备,只需将它们放入应用程序包中,即Android的资产文件夹。您需要myDB.sqlite和文件0/0000000000000001.sqlite文件

6) 现在,您必须在应用程序第一次启动时在应用程序的本地位置复制文件,并确保在第一次SQLite查询之前复制这些文件。要复制这些文件,可以根据您的环境使用以下代码段。JAVA(Android)

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        try {
            String pName = this.getClass().getPackage().getName();
            this.copy("myDB.sqlite", "/data/data/" + pName + "/databases/");
            this.copy("0000000000000001.sqlite", "/data/data/" + pName + "/app_database/file__0/");
        } catch (IOException e) {
            e.printStackTrace();
        }

        super.loadUrl(Config.getStartUrl());
    }

    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();

    }
7) 然后在js文件中添加代码段

    $(document).ready(function(e){      
        document.addEventListener("deviceready", dbConnection, false);            
    }) 

    function dbConnection(){

        db = window.sqlitePlugin.openDatabase("myDB.sqlite", "1.0", "DB", 2000000);
        db.transaction(function(tx) {  
            tx.executeSql("SELECT * from table", [], function(tx, res) {                           
                for(var i=0; i<res.rows.length; i++){                        ;
                    console.log("RESULT:" + res.rows.item(i)['field_name']);
                }            
            });
        });
    }
$(文档).ready(函数(e){
文件。添加的监听器(“DeviceRady”,dbConnection,false);
}) 
函数dbConnection(){
db=window.sqlitePlugin.openDatabase(“myDB.sqlite”,“1.0”,“db”,2000000);
db.事务(功能(tx){
tx.executeSql(“从表中选择*,[],函数(tx,res){
对于(var i=0;i)我可以帮助您
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        try {
            String pName = this.getClass().getPackage().getName();
            this.copy("myDB.sqlite", "/data/data/" + pName + "/databases/");
            this.copy("0000000000000001.sqlite", "/data/data/" + pName + "/app_database/file__0/");
        } catch (IOException e) {
            e.printStackTrace();
        }

        super.loadUrl(Config.getStartUrl());
    }

    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();

    }
    $(document).ready(function(e){      
        document.addEventListener("deviceready", dbConnection, false);            
    }) 

    function dbConnection(){

        db = window.sqlitePlugin.openDatabase("myDB.sqlite", "1.0", "DB", 2000000);
        db.transaction(function(tx) {  
            tx.executeSql("SELECT * from table", [], function(tx, res) {                           
                for(var i=0; i<res.rows.length; i++){                        ;
                    console.log("RESULT:" + res.rows.item(i)['field_name']);
                }            
            });
        });
    }