阿倫學習html5 之Web SQL Database

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   使用   java   ar   for   

不知道什麼情況, W3C不再維護web SQL Database規範,但是大多瀏覽器都支援了!

Web SQL Database規範頁面有著這樣的聲明

Web SQL Database 規範中定義的三個核心方法:
  1. openDatabase:這個方法使用現有資料庫或建立資料庫來建立資料庫物件
  2. transaction:這個方法允許我們根據情況控制事務提交或復原
  3. executeSql:這個方法用於執行SQL 查詢
openDatabase:

我們可以使用這樣簡單的一條語句,建立或開啟一個本地的資料庫物件

var db = openDatabase(‘testDB‘, ‘1.0‘, ‘Test DB‘, 2 * 1024 * 1024);

openDatabase接收五個參數:

  1. 資料庫名字
  2. 資料庫版本號碼
  3. 顯示名字
  4. 資料庫儲存資料的大小(以位元組為單位 )
  5. 回呼函數(非必須)

如果提供了回呼函數,回呼函數用以調用 changeVersion() 函數,不管給定什麼樣的版本號碼,回呼函數將把資料庫的版本號碼設定為空白。如果沒有提供回呼函數,則以給定的版本號碼建立資料庫。

transaction

transaction方法用以處理事務,當一條語句執行失敗的時候,整個交易回復。方法有三個參數

  1. 包含事務內容的一個方法
  2. 執行成功回呼函數(可選)
  3. 執行失敗回呼函數(可選)
executeSql

executeSql方法用以執行SQL語句,返回結果,方法有四個參數

  1. 查詢字串
  2. 用以替換查詢字串中問號的參數
  3. 執行成功回呼函數(可選)
  4. 執行失敗回呼函數(可選)

 

 1 <!DOCTYPE HTML> 2 <html> 3 <head> 4     <title>Web SQL Database</title> 5 </head> 6 <body> 7     <script type="text/javascript"> 8         var db = openDatabase(‘testDB‘, ‘1.0‘, ‘Test DB‘, 2 * 1024 * 1024); 9         var msg;10         db.transaction(function (context) {11            context.executeSql(‘CREATE TABLE IF NOT EXISTS testTable (id unique, name)‘);12            context.executeSql(‘INSERT INTO testTable (id, name) VALUES (0, "Byron")‘);13            context.executeSql(‘INSERT INTO testTable (id, name) VALUES (1, "Casper")‘);14            context.executeSql(‘INSERT INTO testTable (id, name) VALUES (2, "Frank")‘);15          });16 17         db.transaction(function (context) {18            context.executeSql(‘SELECT * FROM testTable‘, [], function (context, results) {19             var len = results.rows.length, i;20             console.log(‘Got ‘+len+‘ rows.‘);21                for (i = 0; i < len; i++){22               console.log(‘id: ‘+results.rows.item(i).id);23               console.log(‘name: ‘+results.rows.item(i).name);24             }25          });26         });27     </script>28 </body>29 </html>

 

阿倫學習html5 之Web SQL Database

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.