Categories
Mobile

mobile: SQL client side database storage

You hear it right: it is available on Safari on the iphone… Example here:

http://onsubject.com/mobile_examples/iphone_local_storage/

Users have 5MB default db size, after that the app will ask them if they want to increase it. Here’s an example on how to create one:

var shortName = ‘Kilo’; var version = ‘1.0’; var displayName = ‘Kilo’; var maxSize = 65536; db = openDatabase(shortName, version, displayName, maxSize); db.transaction( function(transaction) { transaction.executeSql( ‘CREATE TABLE IF NOT EXISTS entries ‘ + ‘ (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ‘ + ‘ date DATE NOT NULL, food TEXT NOT NULL, ‘ + ‘ calories INTEGER NOT NULL );’ ); } );

On your Safari browser, you will be able to see it under: Settings→Safari→Databases, or in the desktop side: Develop→Show Web Inspector and then databases

Here’s an example of inserting records, with a callback function, and error handling:

db.transaction( function(transaction) { transaction.executeSql( ‘INSERT INTO entries (date, calories, food) VALUES (?, ?, ?);’, [date, calories, food], function(){ refreshEntries(); jQT.goBack(); }, errorHandler ); }

function errorHandler(transaction, error) {
    alert('Oops. Error was '+error.message+' (Code '+error.code+')');
    return true;
}

Example of deleting a row:

function deleteEntryById(id) {
    db.transaction(
        function(transaction) {
            transaction.executeSql('DELETE FROM entries WHERE id=?;', 
              [id], null, errorHandler);
        }
    );
}

Leave a Reply

Your email address will not be published. Required fields are marked *