最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error processing SQL: TypeError: window.openDatabase is not a function - Stack Overflow

programmeradmin1浏览0评论

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

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

Issue is here ,

  • Working : Chrome
  • Not Working : Android Mobile & Fire Fox

Code are as below :

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

<!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 FIDDLE Please help me i don't know where i am wrong.

Thanks for read my query.

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

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

Issue is here ,

  • Working : Chrome
  • Not Working : Android Mobile & Fire Fox

Code are as below :

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

<!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 FIDDLE Please help me i don't know where i am wrong.

Thanks for read my query.

Share Improve this question asked Sep 4, 2013 at 4:50 Chintan KhetiyaChintan Khetiya 16.2k9 gold badges51 silver badges85 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

The code "works" in this updated fiddle on Android Browser 4.1.2.

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.

So, I have save it locally in 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>

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

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

发布评论

评论列表(0)

  1. 暂无评论