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.15k stars 716 forks source link

Getting "invalid database handle" at transaction method in Windows 8 #278

Open dcp3450 opened 9 years ago

dcp3450 commented 9 years ago

I'm working on a collaborative project through git hub and on of the developers just pulled down the my repo and attempted to run the files in the exact same manner I do.

My app builds out and creates my db file fine but he's getting a "invalid database handle" when it attempts to start the transaction layer.

nanoDB = window.sqlitePlugin.openDatabase({ name: "nanoDB.db" });
nanoDB.transaction(function (tx) {
    ... execute sql commands ...
},function(err){
    console.log(err);
});

The script never gets to execute the executeSql commands and fails at nanoDB.transaction. We check the location where the database should have been created but it's missing. It seems the "invalid database handle" is telling me that the database doesn't exist. We are both running the same version of windows and Visual Studio so I'm not sure where the hickup could be.

Here's the console log. Sensitive data replaced with [...]

db name: nanoDB.db at full path: C:\Users\[...]\AppData\Local\Packages\io.cordova.myapp2cbd40ba89324213b0f173b57a0bd114_54dbk1tt7zbhy\LocalState\nanoDB.db
new transaction is waiting for open operation
OPEN database: nanoDB.db failed, aborting any pending transactions
Could not open database
Error: Invalid database handle
   {
      [functions]: ,
      __proto__: { },
      code: 0,
      description: "Invalid database handle",
      message: "Invalid database handle",
      name: "Error"
   }
brodycj commented 9 years ago

I suggest you make a small test project and have the other developer try to run it. If it does not work then post a link and I can take a look. You can also send it privately to info@litehelpers.net.

dcp3450 commented 9 years ago

It's just odd. I can build out fine on my machine where I created the project but my other developer can't do anything with the database. I even gave him my DB file to place at the correct location but it still doesn't work.

denkomanceski commented 9 years ago

I also have the same problem when using it on Windows ARM

brodycj commented 9 years ago

@denkomanceski I suggest you try opening a "memory" database (that is not stored on the file system) and try a string test. You would have to edit src/windows/SQLiteProxy.js as follows:

index 74985c9..a410633 100644
--- a/src/windows/SQLiteProxy.js
+++ b/src/windows/SQLiteProxy.js
@@ -25,12 +25,7 @@ module.exports = {
            var res;

                function openImmediate(dbname) {
-                       //var dbname = options.name;
-                       // from @EionRobb / phonegap-win8-sqlite:
-                       var opendbname = Windows.Storage.ApplicationData.current.localFolder.path + "\\" + dbname;
-                       console.log("open db name: " + dbname + " at full path: " + opendbname);
-
-                       var db = new SQLite3JS.Database(opendbname);
+                       var db = new SQLite3JS.Database(":memory:");
                        dbmap[dbname] = db;
                        nextTick(function() {
                                win();

Then simply try the following test program:

window.sqlitePlugin.openDatabase({name:"ignored"}, function(db) {
  db.executeSql("SELECT LENGTH('tenletters') AS stringlength", [], function (res) {
    console.log('got stringlength: ' + res.rows.item(0).stringlength);
  }, function(error) {
    console.log('SELECT error: ' + error.message);
  });
}, function(e) {
  console.log('open database error: ' + JSON.stringify(e));
});
denkomanceski commented 9 years ago

I tried this on a brand new project without any other plugin, now the error is a bit different:

{"DESCRIPTION":"COULD NOT OPEN DATABASE", "STACK":"ERROR: COULD NOT OPEN DATABASE\N
 AT NEWSQLERROR (MS-APPX-WEB://COM.DENKO.TEST/WWW/PLUGINS/CORDOVA-SQLITE-STORAGE/WWW/SQLITEPLUGIN.JS:25:7)\N
 AT ANONYMOUS FUNCTION (MS-APPX-WEB://COM.DENKO.TEST/WWW/PLUGINS/CORDOVA-SQLITE-STORAGE/WWW/SQLITEPLUGIN.JS:197:13)\N
 AT CORDOVA.CALLBACKFROMNATIVE (MS-APPX-WEB://COM.DENKO.TEST/WWW/CORDOVA.JS:295:38)\N
 AT CORDOVA.CALLBACKERROR (MS-APPX-WEB://COM.DENKO.TEST/WWW/CORDOVA.JS:282:9)\N
 AT ONERROR (MS-APPX-WEB://COM.DENKO.TEST/WWW/CORDOVA.JS:895:17)\N
 AT ANONYMOUS FUNCTION (MS-APPX-WEB://COM.DENKO.TEST/WWW/PLUGINS/CORDOVA-SQLITE-STORAGE/SRC/WINDOWS/SQLITEPROXY.JS:49:5)","CODE":0}
brodycj commented 9 years ago

I will give you some more things for you to try in the next 1-2 days.

denkomanceski commented 9 years ago

Great, thanks a lot for the quick response.

Edit: I tried the "in memory database" on normal Windows 10 machine (Local machine), it works fine, no errors

wlkns commented 8 years ago

+1

I am also getting this error, I will try and diagnose, and provide more information (Android 5.1.1).

Would my database name be causing issues?

window.sqlitePlugin.openDatabase({name: "user_1234.db", location: 2, bgType: 1});
imranariffin commented 4 years ago

I had this error once, and later I found out that it was because I manually deleted the *.db file in /data/data/com.<projectname>.debug/databases/<dbname>.db using adb shell. After I deleted the app and installed it again, it didn't see this error anymore.

> react-native info
System:
    OS: Linux 4.15 Pop!_OS 18.04 LTS
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 1.00 GB / 15.55 GB
    Shell: 4.4.19 - /bin/bash
  Binaries:
    Node: 10.14.2 - /usr/bin/node
    Yarn: 1.12.3 - /usr/bin/yarn
    npm: 6.4.1 - /usr/bin/npm
  SDKs:
    Android SDK:
      API Levels: 27, 28
      Build Tools: 27.0.3, 28.0.2, 28.0.3
      System Images: android-23 | Google APIs Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom
  IDEs:
    Android Studio: 3.2 AI-181.5540.7.32.5056338
  npmPackages:
    react: 16.8.6 => 16.8.6 
    react-native: 0.60.5 => 0.60.5

Hope this helps someone.