2012-05-09 50 views

回答

2

正常的Web数据库API可用:http://www.w3.org/TR/webdatabase/

注意:不是所有浏览器都支持Web SQL http://caniuse.com/#feat=sql-storage

例如,我们运行一个测试是与此类似:

var db = openDatabase('mydb', '1.0', 'example database', 2 * 1024 * 1024); 
db.transaction(function (tx) { 
    tx.executeSql('CREATE TABLE IF NOT EXISTS foo (id unique, text)'); 
    tx.executeSql('INSERT INTO foo (id, text) VALUES (1, "foobar")'); 
}); 

db.transaction(function (tx) { 
    tx.executeSql('DROP TABLE foo'); 

    // known to fail - so should rollback the DROP statement 
    tx.executeSql('INSERT INTO foo (id, text) VALUES (1, "foobar")'); 
    forge.logging.error("INSERT into non-existent table succeeded!"); 
}, function (err) { 
    forge.logging.info("error callback invoked, as expected"); 
}); 

db.transaction(function (tx) { 
    tx.executeSql('SELECT * FROM foo', [], function (tx, results) { 
     forge.logging.info("row: "+results); 
    }); 
}); 
+1

感谢您的帮助。将试用 – user567666

+0

标记为已标记的规范。还有其他方法吗? –

+0

IndexedDB将成为替代品:http://www.w3.org/TR/IndexedDB/但它尚未成为股票移动浏览器:http://caniuse.com/indexeddb –

0

现在你应该使用像LocalForage这样的通过indexedDB到webSQL到localStorage的fallback,再加上给你一个一致的API。如果您使用Angular/Ionic,那么这是业务:Angular-LocalForage