litehelpers / Cordova-SQLitePlugin-legacy-iOS-broken-bug666

Cordova/PhoneGap SQLitePlugin for iOS as of September 2013, no longer supported - BROKEN due to BUG litehelpers/Cordova-sqlite-storage#666
89 stars 30 forks source link

Attempting to call Phonegap.exec() before 'ondeviceready' #15

Closed maarcosd closed 12 years ago

maarcosd commented 12 years ago

Hello guys,

I was using thw HTML5 SQL API and it was working fine. Now I decided to start using this plugin, and I got a problem. When I make reference SQLitePlugin.js on my index.html I still dont get any alert, but when I change the "window.openDatabase" to "window.sqlitePlugin.openDatabase" (as in the example), I get the alert "ERROR: Attempting to call Phonegap.exe() before 'deviceready'. Ignoring". Am I missing something?

Sorry if it is a fool question and sorry for poor English :) Thanks in advance

brodybits commented 12 years ago

Many of the Cordova/PhoneGap samples are showing us to wait for the deviceready event, I believe this is the indication that all of the Cordova/PhoneGap facilities are initialized and ready to work with the device. It is good practice not to do anything that depends on persistant storage until we receive this deviceready event.

It might be possible to start working to open a database before receiving the deviceready event but I do not see much reason to mess around with this.

maarcosd commented 12 years ago

The problem is that I call the openDatabase function that is called on 'ondeviceready'. I tried putting a setTimeout of 10 seconds after deviceready and I still get this message, that's what is not making sense, because everything works fine until I call "sqlitePlugin.openDatabase" instead of "openDatabase"

brodybits commented 12 years ago

Can you post a sample?

maarcosd commented 12 years ago

I don't know if it helps, but I am testing on a Galaxy SII running Android 4.0.4.

brodybits commented 12 years ago

Thanks for the information. I do not have much time to look at this today, maybe later tonight. I would like to make a suggestion that you go through the sample and make it as simple as possible while demonstrating the issue.

maarcosd commented 12 years ago

Cool, I deleted the other code to make the thread cleaner. You can see below a simple test I have built that still gets the message:

<html> 
    <head>
        <script type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.1.0.min.js"></script>
        <script type="text/javascript" charset="utf-8" src="js/phonegap.js"></script> 
        <script type="text/javascript" src="js/cordova-1.6.1.js"></script>
        <script type="text/javascript" src="js/SQLitePlugin.js"></script>

        <script type="text/javascript" charset="utf-8">
            function startApp() {
                db = window.sqlitePlugin.openDatabase('test', "1.0", "test", 200000);
            }

            $(document).ready(function() {
                document.addEventListener("deviceready", startApp, true);
            });
        </script>
    </head> 
    <body>
        Testing
    </body> 
</html>
brodybits commented 12 years ago

@marcosd I do not seem to reproduce your problem. I have seen some messages that there could be an issue between Cordova and JQM 1.1.0 on the deviceready event but it does not seem to affect me in my environment. I have tried with API level 16 (4.0.x) on 4.0.3 simulator. I will put my test in the following comment.

brodybits commented 12 years ago

@marcosd here is the test I am doing and it is working. First my index.html:

<!DOCTYPE HTML>
<html>
  <head>
  <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
  <script type="text/javascript" charset="utf-8" src="js/jquery.mobile-1.1.0.js"></script>
  <script type="text/javascript" charset="utf-8" src="js/cordova-1.6.1.js"></script>
  <script type="text/javascript" charset="utf-8" src="js/SQLitePlugin.js"></script>

  <script type="text/javascript" charset="utf-8" >

    function startApp() {
        var db = window.sqlitePlugin.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);

  db.transaction(function(tx) {

  tx.executeSql('DROP TABLE IF EXISTS test_table');
  tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');

  return tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
    console.log("insertId: " + res.insertId + " -- probably 1");
    console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");

    tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
      console.log("rows.length: " + res.rows.length + " -- should be 1");
      //return console.log("rows[0].cnt: " + res.rows.item(0).cnt + " -- should be 1");
      alert("rows[0].cnt: " + res.rows.item(0).cnt + " -- should be 1");
    });

  }, function(e) {
    return console.log("ERROR: " + e.message);
  });
});

    }

    $(document).ready(function() {
        document.addEventListener("deviceready", startApp, true);
    });

    </script>

  </head>
  <body>
    <h1>Testing</h1>
  </body>
</html>

and you have to use the following patch to SQLitePlugin.js to make the test work:

--- assets/www/SQLitePlugin.js  2012-05-01 22:49:25.000000000 +0200
+++ assets/www/js/SQLitePlugin.js   2012-05-01 22:30:57.000000000 +0200
@@ -59,7 +59,7 @@
     this.trans_id = get_unique_id();
     this.__completed = false;
     this.__submitted = false;
-    this.optimization_no_nested_callbacks = true;
+    this.optimization_no_nested_callbacks = false;
     console.log("SQLitePluginTransaction - this.trans_id:" + this.trans_id);
     transaction_queue[this.trans_id] = [];
     transaction_callback_queue[this.trans_id] = new Object();

I hope you can try this and let me know whether or not it is working for you.

brodybits commented 12 years ago

Also I am using jquery 1.7.2, please can you test with that version?

maarcosd commented 12 years ago

I tried and it didn't work. I changed for jQuery 1.7.2 (I was using 1.6.1), changed the plugin file and tried the exact same code as yours, but it still does not work. Now I have no alerts but the queries are not executed, at least they do not seem to be. Here is what is being shown on the LogCat:

success NULL:DROP TABLE IF EXISTS test_table at file:///android_asset/www/js/SQLitePlugin.js:170
executeSql - add_to_transactionDROP TABLE IF EXISTS test_table at file:///android_asset/www/js/SQLitePlugin.js:179
SQLitePluginTransaction.prototype.executeSql at file:///android_asset/www/js/SQLitePlugin.js:143
success NULL:CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer) at file:///android_asset/www/js/SQLitePlugin.js:170
executeSql - add_to_transactionCREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer) at file:///android_asset/www/js/SQLitePlugin.js:179
SQLitePluginTransaction.prototype.executeSql at file:///android_asset/www/js/SQLitePlugin.js:143
success not null:INTO test_table (data, data_num) VALUES ("test",100) at file:///android_asset/www/js/SQLitePlugin.js:150
executeSql - add_to_transactionINSERT INTO test_table (data, data_num) VALUES (?,?) at file:///android_asset/www/js/SQLitePlugin.js:179
SQLitePluginTransaction.prototype.complete at file:///android_asset/www/js/SQLitePlugin.js:183
Error: Status=2 Message=Class not found at file:///android_asset/www/js/cordova-1.7.0rc1.js:924

I don't know what this error in the end means, but it also appears after the log of the function SQLitePlugin.prototype.open.

Also, the success callback function for the INSERT was not called, neither the error callback function.

All the tests were made with API Level 15 (I don't have the 16 and it does not appear on the SDK Manager for me) and with my phone running 4.0.4. The jQuery version is 1.7.2, the cordova version is 1.7.0rc1 (I also tried with the 1.6.1), and I believe that the SQLitePlugin version is the most recent, I downloaded it two days ago.

Actually I don't know if the problem is in the plugin or in me, but I tried so many things to make it work and it just did not work.

brodybits commented 12 years ago

On Wed, May 2, 2012 at 1:16 PM, marcosd reply@reply.github.com wrote:

I tried and it didn't work. I changed for jQuery 1.7.2 (I was using 1.6.1), changed the plugin file and tried the exact same code as yours, but it still does not work. Now I have no

The test I gave you does not work unless you patch SQLitePlugin.js like I gave you. There is a test on the README that does not need the patch to SQLitePlugin.js. I am happy to make a simpler test for you sometime during the weekend.

Chris

maarcosd commented 12 years ago

No problem, do it when it is convenient for you. I tried the example, I tried patching, not patching, and nothing worked. The message "Class not found" persists.

Thanks

maarcosd commented 12 years ago

You don't need to worry about this any more, I found the solution. I did not know that I need to change my /res/xml/plugins.xml to work. I inserted the line below and now it is working fine.

<plugin name="SQLitePlugin" value="com.phonegap.mypackage.sqlitePlugin.SQLitePlugin"/>

Thanks a lot for the attention :)

brodybits commented 12 years ago

@marcosd thank you for letting me know, that is actually a common gotcha:-)

On Thu, May 3, 2012 at 11:28 AM, marcosd reply@reply.github.com wrote:

You don't need to worry about this any more, I found the solution. I did not know that I need to change my /res/xml/plugins.xml to work. I inserted the line below and now it is working fine.

<plugin name="SQLitePlugin" value="com.phonegap.mypackage.sqlitePlugin.SQLitePlugin"/>

Thanks a lot for the attention :)


Reply to this email directly or view it on GitHub: https://github.com/chbrody/Cordova-SQLitePlugin/issues/15#issuecomment-5483913