2014-04-10 32 views

回答

2

下面是一個簡單的步行通過

//variables 
var db; 
var shortName = 'MyDB'; 
var version = '1.0'; 
var displayName = 'MyDB'; 
var maxSize = 200000; 

// this is called when an error happens in a transaction 
function errorHandler(transaction, error) { 
    alert('Error: ' + error.message + ' code: ' + error.code); 
} 

// this is called when a successful transaction happens 
function successCallBack() { 
    alert("DEBUGGING: success"); 
} 

function nullHandler(){} 

// called when the application loads 
function onBodyLoad(){ 

// This alert is used to make sure the application is loaded correctly 
// you can comment this out once you have the application working 
alert("DEBUGGING: we are in the onBodyLoad() function"); 

if (!window.openDatabase) { 
    // not all mobile devices support databases if it does not, the following alert will display 
    // indicating the device will not be albe to run this application 
alert('Databases are not supported in this browser.'); 
    return; 
} 

// this line tries to open the database base locally on the device 
// if it does not exist, it will create it and return a database object stored in variable db 
db = openDatabase(shortName, version, displayName,maxSize); 

// this line will try to create the table User in the database just created/openned 
db.transaction(function(tx){ 

    // you can uncomment this next line if you want the User table to be empty each time the application runs 
//tx.executeSql('DROP TABLE user',nullHandler,nullHandler); 

    // this line actually creates the table User if it does not exist and sets up the three columns and their types 
// note the UserId column is an auto incrementing column which is useful if you want to pull back distinct rows 
    // easily from the table. 
    tx.executeSql('CREATE TABLE IF NOT EXISTS user(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, password TEXT NOT NULL)',[],nullHandler,errorHandler); },errorHandler,successCallBack); 

} 
-1

首先擊中谷歌create database websql

如果嘗試打開一個不存在的數據庫時,API將動態創建 它爲你。您也不必擔心關閉數據庫 。

var db = openDatabase(dbName, '1.0', dbDescription, dbSize); 

/** 
Params: 

    Database name 
    Version number 
    Text description 
    Estimated size of database 

(來源:http://html5doctor.com/introducing-web-sql-databases/

+0

Downvote?!做什麼的? – DanFromGermany