Without knowing what to do, the Web SQL database specification is no longer maintained, but most browsers support it!
The Web SQL Database specification page has such a declaration
The three core methods defined in the Web SQL Database specification:
- OpenDatabase: This method uses an existing database or creates a new database to create a database object
- Transaction: This method allows us to control transaction commit or rollback based on the situation
- ExecuteSQL: This method is used to execute SQL queries
OpenDatabase:
We can use a simple statement like this to create or open a local database object
var db = OpenDatabase (' TestDB ', ' 1.0 ', ' Test db ', 2 * 1024 * 1024);
OpenDatabase receives five parameters:
- Database name
- Database version number
- Display Name
- The size, in bytes, of the database save data
- callback function (not required)
If a callback function is provided, the callback function calls the Changeversion () function, and the callback function sets the database version number to NULL, regardless of the version number given. If no callback function is provided, the database is created with the given version number.
Transaction
The transaction method is used to handle transactions, and the entire transaction is rolled back when a statement execution fails. Method has three parameters
- A method that contains the contents of a transaction
- Execute successful callback function (optional)
- Execute failure callback function (optional)
ExecuteSQL
The ExecuteSQL method is used to execute the SQL statement, returning the result, with four parameters of the method
- Query string
- parameter to replace question mark in query string
- Execute successful callback function (optional)
- Execute failure callback function (optional)
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title>Web SQL Database</title>5 </Head>6 <Body>7 <Scripttype= "Text/javascript">8 varDB=OpenDatabase ('TestDB', '1.0', 'Test DB', 2 * 1024x768 * 1024x768);9 varmsg;Ten Db.transaction (function(context) { One Context.executesql ('CREATE TABLE IF not EXISTS testtable (ID unique, name)'); A Context.executesql ('INSERT into testtable (ID, name) VALUES (0, "Byron")'); - Context.executesql ('INSERT into testtable (ID, name) VALUES (1, "Casper")'); - Context.executesql ('INSERT into testtable (ID, name) VALUES (2, "Frank")'); the }); - - Db.transaction (function(context) { - Context.executesql ('SELECT * from TestTable', [], function(context, results) { + varLen=results.rows.length, I; - Console.log ('Got'+Len+'rows.'); + for(i= 0; I<Len; I++){ A Console.log ('ID:'+Results.rows.item (i). ID); at Console.log ('Name:'+Results.rows.item (i). name); - } - }); - }); - </Script> - </Body> in </HTML>
Alan learns HTML5 's web SQL Database