How to create Database in webSQL?
this does not answer the question Create database in WebSQL
How to create Database in webSQL?
this does not answer the question Create database in WebSQL
Here is a simple walk-through
//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);
}
First hit on google create database websql
:
If you try to open a database that doesn’t exist, the API will create it on the fly for you. You also don’t have to worry about closing databases.
var db = openDatabase(dbName, '1.0', dbDescription, dbSize);
/**
Params:
Database name
Version number
Text description
Estimated size of database
(Source: http://html5doctor.com/introducing-web-sql-databases/)