javascript 处理 SQL 时出错:TypeError: window.openDatabase is not a function

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18605614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:32:50  来源:igfitidea点击:

Error processing SQL: TypeError: window.openDatabase is not a function

javascriptandroidhtmlcordovaopendatabase

提问by Chintan Khetiya

I have some strange issue in my code. i have create simple DB_Function.jsto manage JS function inside html page.

我的代码中有一些奇怪的问题。我DB_Function.js在 html 页面中创建了简单的JS 函数来管理。

i am calling function inside <body onLoad="CALL_DB()">so first that will initialize the DB and create table if needed.

我在内部调用函数,<body onLoad="CALL_DB()">因此首先将初始化数据库并在需要时创建表。

Issue is here ,

问题在这里,

  • Working : Chrome
  • Not Working : Android Mobile & Fire Fox
  • 工作:铬
  • 不工作:Android Mobile 和 Fire Fox

Code are as below :

代码如下:

DB_Function.js

DB_Function.js

function CALL_DB() {
    try {
        alert("call_db");
        var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
        db.transaction(populateDB, errorCB, successCB);
    } catch (err) {
        alert("Error processing SQL: " + err);
    }
}


function populateDB(tx) {
    try {
        alert("call_table");
        tx.executeSql('DROP TABLE IF EXISTS DEMO');
        tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (3, "C")');
        tx.executeSql('INSERT INTO DEMO (id, data) VALUES (4, "K")');
    } catch (err) {
        alert("Error processing SQL: " + err);
    }
}


function errorCB(tx, err) {
    alert("Error processing SQL: " + err);
}

// Transaction success callback

function successCB() {
    alert("success!");
}

Test.html

测试.html

<!DOCTYPE html>
<html>
  <head>
    <title>Storage Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8" src="js/DB_function.js"></script>

  </head>
  <body onLoad="CALL_DB()">
    <h1>Example</h1>
    <p>Database</p>
  </body>
</html>

Here is the my JS FIDDLEPlease help me i don't know where i am wrong.

这是我的JS FIDDLE请帮助我,我不知道我错在哪里。

Thanks for read my query.

感谢您阅读我的查询。

采纳答案by Chintan Khetiya

Issue was here,

问题在这里,

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>

I have address a live js file to load so might be they was not able to load while launch the application.

我已经解决了要加载的实时 js 文件,因此可能是他们在启动应用程序时无法加载。

So, I have save it locally in assets/www/js/cordova.js.

所以,我把它保存在本地assets/www/js/cordova.js

So finally it looks like above and its working for me perfectly.

所以最后它看起来像上面一样,它对我来说完美无缺。

 <script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>

回答by nmaier

You are using Web SQL Database, which is not supported by Firefox. Also, the specification is abandoned by the W3C.

您正在使用Firefox 不支持的Web SQL 数据库。此外,该规范已被 W3C 放弃

Have a look at IndexedDB(caniuse) instead. There is also at least one shimto make IndexedDB work in browsers that only support Web SQL Database.

看看IndexedDB( caniuse)。还有至少一个shim可以使 IndexedDB 在仅支持 Web SQL 数据库的浏览器中工作。

The code "works" in this updated fiddleon Android Browser 4.1.2.

代码“作品”在这个更新的小提琴Android Browser 4.1.2

回答by AmirServiceRocket

Look like your haven't initialize Cordova / PhoneGap API. You must first listen to deviceReady event prior to manipulate the APIs

看起来您还没有初始化 Cordova / PhoneGap API。在操作 API 之前,您必须首先侦听 deviceReady 事件

Look at the following doc: http://docs.phonegap.com/en/2.9.0/cordova_events_events.md.html#deviceready

查看以下文档:http: //docs.phonegap.com/en/2.9.0/cordova_events_events.md.html#deviceready