Montag, 31. Oktober 2011

console.log

function debug(message) {
if (navigator.appName != 'Microsoft Internet Explorer') {
console.log();
}
}

Sonntag, 30. Oktober 2011

Web SQL Datenbanken

WebSql


Es handelt bei dem nun folgenden Ausführungen um die Beschreibung einer JavaScript-API.
Das bedeutet, dass ich die neuen Funktionen vorstelle.

WebSql unterstützt Transaktionen. Noch besser: Sämtliche SQL-Beefehlet werden immer innerhalb einer Transaktion ausgeführt. Am besten man entwickelt mit dem Google Chrome Browser oder dem Safari. Der Firefox untersützt die API nicht.

Die offizielle spezifikation befindet sich unter http://www.w3.org/TR/webdatabase.

WebSql verwendet den SQL-Dialect von SQLLite http://www.sqlite.org.
Da kann man unter anderen nachlesen welche Datentypen unterstützt werden (INTEGER, REAL, TEXT, BLOB,NULL).

Es gibt laut SQLLite FAQs eine einfache Unterstüzung für ALTER TABLE, das muss ich aber noch ausprobieren.
http://www.sqlite.org/faq.html

Bei der INSERT-Anweisung nach Möglichkeit immer ? verwenden, und nicht dynamisch zusammen bauen. Sonst ist die Anweisung leicht hackbar (SQL-Injection wird möglich)

Bei der w3-school gibt es zwar noch keine Seite, habe aber eine ähnlich gefunden.
http://www.tutorialspoint.com/html5/html5_web_sql.htm














INTEGER
REAL
TEXT
BLOB
NULL











KonstanteCodeBedeutung
UNKNOWN_ERROR0
DATABASE_ERR1
VERSION_ERR2
TOO_LARGE_ERR3
QUOTA_ERR4




CREATE TABLE




CREATE TABLE IF NOT EXISTS notes (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
note TEXT,
date DATE NOT NULL DEFAULT CURRENT_TIMESTAMP
);






DROP TABLE


DROP TABLE notes;



transaction

readTransaction


void transaction(in SQLTransactionCallback callback, in optional SQLTransactionErrorCallback errorCallback, in optional SQLVoidCallback successCallback);
void readTransaction(in SQLTransactionCallback callback, in optional SQLTransactionErrorCallback errorCallback, in optional SQLVoidCallback successCallback);



executeSql


void executeSql(in DOMString sqlStatement, in optional ObjectArray arguments, in optional SQLStatementCallback callback, in optional SQLStatementErrorCallback errorCallback);




Einführung




Es werden APIs vorgestellt mit denen es möglich ist die eingebaute Browser-Datenbank mit SQL zu manipulieren.
Die APIT ist asynchron.





Here is an example of a script using this API. First, a function prepareDatabase() is defined. This function returns a handle to the database, first creating the database if necessary. The example then calls the function to do the actual work, in this case showDocCount().



function prepareDatabase(ready, error) {
return openDatabase('documents', '1.0', 'Offline document storage', 5*1024*1024, function (db) {
db.changeVersion('', '1.0', function (t) {
t.executeSql('CREATE TABLE docids (id, name)');
}, error);
});
}

function showDocCount(db, span) {
db.readTransaction(function (t) {
t.executeSql('SELECT COUNT(*) AS c FROM docids', [], function (t, r) {
span.textContent = r.rows[0].c;
}, function (t, e) {
// couldn't read database
span.textContent = '(unknown: ' + e.message + ')';
});
});
}

prepareDatabase(function(db) {
// got database
var span = document.getElementById('doc-count');
showDocCount(db, span);
}, function (e) {
// error getting database
alert(e.message);
});




The executeSql() method has an argument intended to allow variables to be substituted into statements without risking SQL injection vulnerabilities:



db.readTransaction(function (t) {
t.executeSql('SELECT title, author FROM docs WHERE id=?', [id], function (t, data) {
report(data.rows[0].title, data.rows[0].author);
});
});




XXXX



Database openDatabase(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize, in optional DatabaseCallback creationCallback);
DatabaseSync openDatabaseSync(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize, in optional DatabaseCallback creationCallback);



4.5 Database query results




Die executeSql() Methode ruft einen Callback-Funktion auf die ein SQLResultSet als Argument erwartet.



interface SQLResultSet {
readonly attribute long insertId;
readonly attribute long rowsAffected;
readonly attribute SQLResultSetRowList rows;
};