storesafe / cordova-sqlite-storage

A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API
Other
2.14k stars 713 forks source link

db.sqlBatch( ) run success , but callback not run #989

Open seotkd opened 2 years ago

seotkd commented 2 years ago

The steps are as follows: 1, executeSql() is wrappered in promise 2, run promise( ) , then run batch( )

There was a problem with the operation: in the database, the data insert success, but no console or alert. if don't use promise async, sqlBatch() callback is normal

let promise = new Promise(resolve=>{

    db.executeSql('SELECT count(*) AS mycount FROM DemoTable', [], function(rs) {
        console.log('Record count (expected to be 2): ' + rs.rows.item(0).mycount);
        resolve(true);
      }, function(error) {
        console.log('SELECT SQL statement ERROR: ' + error.message);
    });

})

function batch(){   
    db.sqlBatch([
        'CREATE TABLE IF NOT EXISTS DemoTable (name, score)',
        [ 'INSERT INTO DemoTable VALUES (?,?)', ['Alice', 101] ],
        [ 'INSERT INTO DemoTable VALUES (?,?)', ['Betty', 202] ],
        ], function() {
        console.log('Populated database OK');
                alert("success")
        }, function(error) {
        console.log('SQL batch ERROR: ' + error.message);
               alert("error")
    });  
}

promise().then(res=>{

  if(res === true) batch(); 
  // batch run success, but no console or alert.the callback is invalid.

})

/ *  If only batch() is executed , the callback is very good suach as  */
batch(); // batch run success ,console and alert is valid
skrs13 commented 2 years ago

Hi - I'm also facing a similar issue with executeSql() API. Were you able to fix this or work-around it?

[(https://github.com/storesafe/cordova-sqlite-storage/issues/999#issue-1128856851)]