如何在webSQL中创建新数据库?

时间:2014-04-10 10:01:39

标签: javascript web-sql

如何在webSQL中创建数据库?

这不回答这个问题 Create database in WebSQL

2 个答案:

答案 0 :(得分: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 :(得分:-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/