webSql

http://www.oschina.net/question/12_26204

 

http://www.silverlightchina.net/html/HTML_5/study/2012/1008/19329.html

 

https://www.webkit.org/blog/126/webkit-does-html5-client-side-database-storage/

 

 1 <script>

 2     // openDatabase

 3     function initDatabase() {

 4         try{

 5             if(!window.openDatabase) {

 6                 alert("Databases are not supported in this browser");

 7             }else{

 8                 var shortName = "DEMODB";

 9                 var version = "1.0";

10                 var displayName = "DEMODatabase";

11                 var maxSize = 100000; // bytes

12                 DEMODB = openDatabase(shortName, version, displayName, maxSize);

13                 createTables();

14                 selectAll();

15             }

16         }catch(error) {

17             alert("error.name: " + error.name);

18             alert("error.message: " + error.message);

19             alert("error.number: " + error.number);

20             alert("error.description: " + error.description);

21         }

22     }

23     

24     function createTables() {

25         DEMODB.transaction(

26             function(transaction) {

27                 transaction.executeSql("CREATE TABLE IF NOT EXISTS page_settings(id INTEGER NOT NULL PRIMARY KEY, fname TEXT NOT NULL,bgcolor TEXT NOT NULL, font TEXT, favcar TEXT);", [], nullDataHandler, errorHandler);

28             }

29         );

30         prePopulate();

31     }

32     

33     // insert

34     function prePopulate() {

35         DEMODB.transaction(

36             function (transaction) {

37                 var data = ["1", "none", "#B3B4EF", "Helvetica", "Porsche 911 GT3"];

38                 

39                 transaction.executeSql("Insert into page_settings(id, fname, bgcolor, font, favcar) values(?, ?, ?, ?, ?)", [data[0], data[1], data[2], data[3], data[4]]);

40             }

41         );

42     }

43     

44     function selectAll() {

45         DEMODB.transaction(

46             function (transaction) {

47                 transaction.executeSql("select * from page_settings", [], dataSelectHandler, errorHandler);

48             }

49         );

50     }

51     

52     function dataSelectHandler(transaction, results) {

53         for(var i = 0; i < results.rows.length; i++ ) {

54             var row = results.rows.item(i);

55             var newFeature = new Object();

56             newFeature.id = row["id"];

57             newFeature.fname = row["fname"];

58             newFeature.bgcolor = row["bgcolor"];

59             newFeature.font = row["font"];

60             newFeature.favcar = row["favcar"];

61         }

62         for(i in newFeature) {

63             alert(newFeature[i]);

64         }

65     }

66     

67     function nullDataHandler() {

68     }

69     

70     function errorHandler() {

71     }

72     

73     function updateSetting() {

74         DEMODB.transaction(

75             function(transaction) {

76                 var fname = "roboce";

77                 var bg = "love";

78                 var font = "lisa";

79                 var car = "forever";

80                 transaction.executeSql("update page_settings set fname = ?, bgcolor = ?, font = ?, favcar = ? where id = 1", [fname, bg, font, car]);

81             }

82         );

83     }

84     

85     function dropTable() {

86         DEMODB.transaction(

87             function(transaction) {

88                 transaction.executeSql("drop table page_settings;", [], nullDataHandler, errorHandler);

89             }

90         );

91         

92         console.log("Table 'page_settings' has been dropped.");

93     }

94     

95     initDatabase();

96     updateSetting();

97     dropTable();

98 </script>

 

你可能感兴趣的:(sql)