0

I'm "trying" to create a Safari Extension that adds links to a local sqlite database.

In the global.html file, I can create the database, but I can't create a table unless I refer to a non-existant variable, which makes the rest of the script fail.

// Global Variable to hold my database
var my_DB;

// Make A Database
init_DB();

// Make A Table
createTable(my_DB);

// Rest of the code
alert("Database and Table have been created!");

// Initialise the Database
function init_DB() {    
    try {
        // Check for Database Support
        if (!window.openDatabase) {
            alert("Database functionality is NOT Supported!");
        } else {
            // Setup the database
            var shortName = "imp_DB";
            var version = '1.0';
            var displayName = "My Important Database";
            var maxSize = 65536; // in bytes
            var theDB = openDatabase(shortName, version, displayName, maxSize);
        }
    } catch(e) {
        // Error Handling
        if (e == "INVALID_STATE_ERR") {
            // We have a version number mismatch
            alert("Invalid database version");
        } else {
            // Unknown error
            alert("Unknown error: " + e);
        }
        return;
    }
    // Assign the database to the global variable
    my_DB = theDB;
}


// Create The Table
function createTable(thisDB) {
    thisDB.transaction(function(txn) {
        txn.executeSql('CREATE TABLE IF NOT EXISTS people (id unique, name, age)');
    });
    // The following line is the problem
    someVar = txn;
}

Calling init_DB() works fine, and the database is created. However, calling createTable(my_DB) just fails silently, (No Errors, or Warnings) and the rest of the code completes, UNLESS I refer to "txn" somehow.

So adding the "SomeVar = txn;" line allows the Table to be created, but unfortunately, it causes "ReferenceError: Can't find variable: txn" to appear in the console, and stops the rest of the code from running.

Does anyone have any ideas? I've been trying to get this to work for over a week, and I'm at my wit's end.

Thanks in advance for any suggestions ;-)

N.B. Declaring "txn" before attempting to create a Table also causes a silent fail.

cybernanga
  • 11
  • 1
  • Two comments. 1) How do you know the table creation failed? 2) You can't use `txn` outside the anonymous function that is the argument of `thisDB.transaction`; that variable is local to that function. – chulster Jul 08 '12 at 15:37
  • Hi @canisbos, I'm using 2 methods to view the database while testing: a) The Resources Tab of Safari's Web Inspector for the Global Page b) The SQLite Manager plugin for Firefox. I realise I shouldn't use "txn" outside of the anonymous function, that's what is causing the rest of the code to fail. However, I can't get any queries to work unless I do, which means I can't proceed with the rest of the code. – cybernanga Jul 08 '12 at 17:16
  • Weird. Using exactly your code, minus the `someVar = txn;` line, I see the table successfully created. I am using the Storage tab of Safari 5.2's web inspector to check the database. The table also shows up when I open the database in Base (a desktop app). – chulster Jul 08 '12 at 21:09
  • Was your test done in a normal page, or as part of a Safari Extension? As I have found that it works fine in a Standalone page (without the someVar bit), but fails when in the global page of an Extension. – cybernanga Jul 08 '12 at 22:27
  • I did the test in Safari's error console for the global page of an extension. – chulster Jul 09 '12 at 02:00

0 Answers0