lundi 24 août 2015

Unable to “Insert, Delete & Update” a table “ chkdmp” using a file “GExists.sql” in Javascript code in “GExistsChanges1.html”

I am Getting the following errors:

XMLHttpRequest cannot load file:///E:/A%20Semi%20-%20Static/Testing/Testing%20Database%20connection%20with%20SQLite3/GExists.sql. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///E:/A%20Semi%20-%20Static/Testing/Testing%20Database%20connection%20with%20SQLite3/GExists.sql'.
 in running the following html “GExistsChanges1.html” 

when I run the following “GExistsChanges1.html” in Google chrome.

 <!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See http://ift.tt/1hFCJLr -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
      <!--  <link rel="stylesheet" type="text/css" href="css/index.css" /> -->

        <!--The Jquery and cordova files -->
        <!-- <script type="text/javascript" src="cordova.js"></script> -->
        <script src="js/jquery-1.11.3.min.js"></script>
        <script src="js/jquery.min.js"></script>
 
        <title>CRAYZO</title>
    </head>
    <body onload="onBodyLoad()">
                
        <script type="text/javascript">

        var db;
        // this is called when an error happens in a transaction
        function errorHandler(transaction, error) {
             alert('Error: ' + error.message + ' code: ' + error.code);
        }

        // this is called when a successful transaction happens
        function successCallBack() {
           alert("DEBUGGING: success");
        }

        function nullHandler(){};
        // called when the application loads
        function onBodyLoad() {

            alert("DEBUGGING: we are in the onBodyLoad() function");

            $.get('GExists.sql', function (response) {
                var db = openDatabase('GExists', '1.0', 'myDatabase', 10000000);
            });
            
            // list the values in the database to the screen using jquery to update the #lbUsers element
            function ListDBValues() {

                if (!window.openDatabase) {
                    alert('Databases are not supported in this browser.');
                    return;
                }

                // this line clears out any content in the #lbUsers element on the page so that the next few lines will show updated
                // content and not just keep repeating lines
                $('#lbUsers').html('');

                // this next section will select all the content from the User table and then go through it row by row
                // appending the UserId  FirstName  LastName to the  #lbUsers element on the page
                db.transaction(function (transaction) {
                    transaction.executeSql('SELECT * FROM chkdmp;', [], function (transaction, result) {
                        if (result != null && result.rows != null) {
                            for (var i = 0; i < result.rows.length; i++) {
                                var row = result.rows.item(i);
                                $('#lbUsers').append('<br>' + row.UserId + '. ' + row.FirstName + ' ' + row.LastName);
                            }
                        }
                    });
                });

                return;

            }

            // this is the function that puts values into the database using the values from the text boxes on the screen
            function AddValueToDB() {

                if (!window.openDatabase) {
                    alert('Databases are not supported in this browser.');
                    return;
                }

                // this is the section that actually inserts the values into the User         table
                db.transaction(function (transaction) {
                    transaction.executeSql('INSERT INTO chkdmp(UserId,FirstName, LastName) VALUES (?,?,?)', [$('#txUserId').val(), $('#txFirstName').val(), $('#txLastName').val()], nullHandler, errorHandler);
                });

                // this calls the function that will show what is in the User table in the database
                ListDBValues();

                return false;

            }
           
        }</script>

            <input id="txUserId" type="text" placeholder="UserId">
        <input id="txFirstName" type="text" placeholder="FirstName">
        <input id="txLastName" type="text" placeholder="Last Name">
        <input type="button" value="Add record" onClick="AddValueToDB()"> 
        <input type="button" value="Read" onClick="ListDBValues()"> <br>
        <br>
        <span style="font-weight:bold;">Currently stored values:</span>
        <span id="lbUsers"></span>

   </body> 
</html>

The problem however is that I am trying to get, database "GExists.db" loaded from “GExists.sql” through the code snippet below, that is present in the above HTML “GExistsChanges1.html”

$.get('GExists.sql', function (response) {
                var db = openDatabase('GExists', '1.0', 'myDatabase', 10000000);
            });

I have put SQLite database file onto “GExists.sql” by the following commands on the SQLite3 command prompt

.output GExists.sql
.dump

The contents of the file “GExists.sql” are as follows

PRAGMA foreign_keys=OFF;BEGIN TRANSACTION;CREATE TABLE chkdmp(UserId INTEGER NOT NULL , FirstName TEXT NOT NULL, LastName TEXT NOT NULL);INSERT INTO "chkdmp" VALUES(1,'Jaselina','Patwar');INSERT INTO "chkdmp" VALUES(2,'Gluben','Malwa');INSERT INTO "chkdmp" VALUES(3,'Katra','Pullet');
COMMIT;

Ultimately, I am not able to

“Insert, Delete & Update” a table by name “chkdmp” using a pre-existing dumped file “GExists.sql” in Javascript code in “GExistsChanges1.html”

If it runs successfully I will make an apk to run it on Android device.

Please help

Aucun commentaire:

Enregistrer un commentaire