【问题标题】:Phonegap CRUD operations - window.openDatabasePhonegap CRUD 操作 - window.openDatabase
【发布时间】:2018-09-11 19:30:47
【问题描述】:

我正在尝试使用 phonegap/cordova 创建 CRUD 操作,但我不断收到“未捕获的 DOMException:试图突破用户代理的安全策略。在 window.onload - 第 17 行”指向 => "db = window.openDatabase("employee", "1.1", "LearnToProgram", 200000);".这进一步影响 db.transaction 第 25 行“Uncaught TypeError: Cannot read property 'transaction' of undefined”,因为没有创建数据库。我是否需要任何 SQLLite 插件,也许还缺少其他东西?

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title>SQLLite DB App</title>
          <script type="text/javascript" src="cordova.js"></script>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
        <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
        <script>
        var db;

        window.onload=function()
        {
            document.getElementById('btnSave').addEventListener('click', saveData);
            db = window.openDatabase("employees",  "1.0", "LearnToProgram", 200000);
        }

        function saveData(e)
        {



            db.transaction(saveRecord, onSuccess, onError);
        }

        function saveRecord(transaction)
        {
            var name= document.getElementById('name').value;
            var email = document.getElementById('email').value;
            transaction.executeSql('CREATE TABLE IF NOT EXISTS employeesList (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, Email TEXT NOT NULL) ');

            var sql= "INSERT INTO employeesList (Name,Email) VALUES ('" + name +"', '" + email + "')";
            console.log(sql);
            transaction.executeSql(sql);
        }

        function onSuccess()
        {
            console.log("Record Saved");
        }

        function onError(error)
        {
            console.log("SQL error: " + error.code);
        }
        </script>
    </head>
    <body>
        <div data-role="page">
        <div data-role="header"><h1>Database Storage</h1></div>
        <div data-role="main" class="ui-content">
        <label for="name">Name</label>
        <input type="text" id="name" />
        <label for="email">Email</label>
        <input type="text" id="email" />
        <button id="btnSave" type="submit">Save</button>
        <button id="showList">Show Employees</button>

        </div><!-- main-->
        </div><!-- page -->
    </body>
</html>

【问题讨论】:

    标签: sqlite cordova phonegap


    【解决方案1】:

    也许我能帮你。

    打开数据库:

    var db = null;
    
    document.addEventListener('deviceready', function() {
      db = window.sqlitePlugin.openDatabase({
        name: 'my.db',
        location: 'default',
      });
    });
    

    重要提示:与其他 Cordova/phonegap 插件一样,您的应用程序必须等待 deviceready 事件。这在 Angular/ngCordova/Ionic 控制器/工厂/服务回调中尤其棘手,可能在触发 deviceready 事件之前触发。

    【讨论】:

    • 我实现了类似的东西,但仍然给出相同的错误 document.addEventListener("deviceready", onDeviceReady, false);变量数据库; function onDeviceReady() { //alert("设备就绪"); db = window.openDatabase("员工", "1.0", "LearnToProgram", 200000); db.transaction(createDB, errorCB, successCB); }
    • 是的,我使用警报进行了检查,并且收到了它。问题是为什么没有创建数据库
    • 检查了该链接,它与我所寻求的有点不同。我认为我的问题是为什么没有创建数据库。加载活动还可以
    • 还在吗?你以前做过类似的东西吗
    猜你喜欢
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多