storesafe / cordova-sqlite-evcore-extbuild-free

Cordova sqlite plugin with Android performance enhancements for PhoneGap Build, GPL v3 or commercial license options
Other
24 stars 13 forks source link

Unable to open db using androidDatabaseLocation: directoryEntry.toURL() #67

Closed eb1 closed 9 months ago

eb1 commented 9 months ago

This is a weird one. I recently upgraded the cordova-plugin-file to 8.0.0 and cordova-android to 12.0.1. Somewhere along the way I lost the ability to open my sqlite db from the cordova.file.dataDirectory. Code:

    db_dir = cordova.file.dataDirectory;
    console.log("db_dir: " + db_dir);
    // now attempt to get the directory
    window.resolveLocalFileSystemURL(db_dir, function (directoryEntry) {
        console.log("Got directoryEntry. Attempting to create / open DB at: " + directoryEntry.toURL());
        // Attempt to create / open our database now
        window.Application.db = window.sqlitePlugin.openDatabase({name: DB_NAME, androidDatabaseLocation: directoryEntry.toURL()}, function(db) {
            // do something cool here
        }, function (err) {
             console.log("Open database ERROR: " + JSON.stringify(err));
         });
         }, function (err) {
             console.log("resolveLocalFileSustemURL error: " + err.message);
         });

console output:

db_dir: file:///data/user/0/<my app>/files/
Application.js:227 Got directoryEntry. Attempting to create / open DB at: https://localhost/__cdvfile_files__/
SQLitePlugin.js:185 OPEN database: AIM
SQLitePlugin.js:211 OPEN database: AIM FAILED, aborting any pending transactions
Application.js:232 Open database ERROR: {"code":0}

Note that setting the db location to default ( location: 'default' ) works properly. I'm guessing something changed in the way the resolveLocalFileSystemURL creates the directoryEntry?

eb1 commented 9 months ago

Oh, it's definitely due to something changing in the file plugin:

cordova-plugin-file@6.0.2 directoryEntry.toURL(): file:///data/user/0/<my app>/files/ cordova-plugin-file@8.0.0 directoryEntry.toURL(): https://localhost/__cdvfile_files__/

Not sure what the guidance is here, since the toURL() call is returning different values between versions of the file plugin. I'll try pinging the dev Slack chat.

eb1 commented 9 months ago

Response from the dev team Slack chat: "historical toURL was for embedding URLS in the dom and with all the scheme stuff, file/cdvfile doesn't work since its cross-origin. That's why toURL now returns a https:// url on android. I think there is a .nativeURL property if I recall correctly that should be the file:/// path"

Just tried passing in the .nativeurl property and the database does open correctly:

window.Application.db = window.sqlitePlugin.openDatabase({name: DB_NAME, androidDatabaseLocation: directoryEntry.nativeurl}, function(db) {...}

I'm not sure how far back the .nativeurl property goes, but it's probably worth updating the readme.md for this plugin to indicate that's how to open the DB in a custom location starting with v. 8.0 of the file plugin. I'll mark this as closed, in case someone runs into the same issue. Thanks @brodybits!