0

I want to use sqlite db for my iOS application using phone gap. I add sqlite plugins, linked sqlite libraries. Did everything correct, when I run my app in emulator iOS 6.1, it launches fine but not giving any alert, I want to display on db success etc. Also I don't know if database is created successfully, where can I find that db file. Here is my code. Thanks in advance.

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

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

            // Wait for Cordova to load
            //
            document.addEventListener("deviceready", onDeviceReady, false);

            // Cordova is ready
            //
            function onDeviceReady() {

                 alert("correct");
                var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
                db.transaction(populateDB, errorCB, successCB);
            }

        // 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")');
        }

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

        // Transaction success callback
        //
        function successCB() {
            alert("success!");
        }

            </script>
    </head>
    <body>
        <h1>Example</h1>
        <p>Database</p>
    </body>
</html>
Wasim Ahmad
  • 71
  • 1
  • 8

1 Answers1

0

The cordova.js file is normally in the <project_name>\platforms\android\assets\www directory.
You have shown it to be in <project_name>\platforms\android\assets\www\js as per your code.

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

You need to check the correct path of your cordova.js file. If it is in www directory than you need to change the script tag as below.

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

Is the onDeviceReady() function being fired?
is the alert("correct") being called?

frank
  • 3,180
  • 3
  • 16
  • 18
  • cordova.js is in js folder inside www directly. I think onDeviceReady() function not being called neither throwing any alert. – Wasim Ahmad Dec 11 '14 at 09:08
  • can you try to **comment** out `lawnchair.js, SQLiteplugin.js and lawnchair-sqliteplugin.js` and check whether it works?. The cordova app will use built-in WebSQL as the sqlite db. – frank Dec 11 '14 at 09:12
  • now i imported the latest cordova.js for iOS, it gives the success alert. Now my question is where i can find my db file, which is created now – Wasim Ahmad Dec 11 '14 at 09:23
  • have a look over [here](http://stackoverflow.com/questions/1108076/where-does-the-iphone-simulator-store-its-data) for app data storage location. – frank Dec 11 '14 at 09:45
  • it does not give a clear way to find the directory where database is created – Wasim Ahmad Dec 11 '14 at 12:57