简体   繁体   中英

How to read the data from sqlite3 in javascript in phonegap application for iphone

I am working on an application in phonegap for iphone, where i have a database in sqlite3 which contains values of latitudes and longitudes and corresponding there area names. I have copied the sqlite data file within the application. I need to fetch the data from the sqlite and display the data in the table in the view, so how to fetch the data in the javascript?

@PiTheNumber try that code

         var arr = new Array(); 
        // Wait for PhoneGap to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // Populate the database 
        //
        function populateDB(tx) {
            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 (1, "First row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (3, "First row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (4, "Second row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (5, "First row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (6, "Second row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (7, "First row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (8, "Second row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (9, "First row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (10, "Second row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (11, "First row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (12, "Second row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (13, "First row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (14, "Second row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (15, "First row")');
            tx.executeSql('INSERT INTO DEMO (id, data) VALUES (16, "Second row")');
        }



        // Query the database
        //
        function queryDB(tx) {
            tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
        }

        // Query the success callback
        //
        function querySuccess(tx, results) {
            var len = results.rows.length;
            console.log("DEMO table: " + len + " rows found.");
            for (var i=0; i<len; i++){
            $('#home').append('<li><a href="#artists">'+results.rows.item(i).data+'</a></li>');



            }
            console.log("aaa"+arr+"aaa"); 

        }

        // Transaction error callback
        //
        function errorCB(err) {
            console.log("Error processing SQL: "+err.code);
        }

        // Transaction success callback
        //
        function successCB() {
            var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
            db.transaction(queryDB, errorCB);
        }

        // PhoneGap is ready
        //
        function onDeviceReady() {
            var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
            db.transaction(populateDB, errorCB, successCB);
        }

@shasha replace the refrence of phonegap.js" file with phonegap-1.4.1.js

(if u are using 1.4.1 version of phone gap) it will work.

@InderKumarRathore dude i just figured it out this was my table view or ur can say list view

        <body>
            <div class="toolbar">
                <h1 id="pageTitle"></h1>
                <a id="backButton" class="button" href="#"></a>
                <a class="button" href="#searchForm">Search</a>
            </div>

            <ul id="home" title="Music" selected="true">
                <li><a href="#artists">Artists</a></li>
                <li><a href="#settings">Settings</a></li>
                <li><a href="stats.gtpl">Stats</a></li>
                <li><a href="#themes">Theme Switcher</a></li>
                <li><a href="#iui-cache-panel">HTML5 WebApp Cache</a></li>
                <li><a href="http://code.google.com/p/iui/" target="_self">About</a></li>
                <li>Nothing</li>
            </ul>    </body>

and in Java script use this code

$('#home').append('<li>'+data+'</li>');

data will the the variable u want to use

hope it worked it does for me... !!!!

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM