Sqllite file-type SQL database that can be accessed through the SQL language

Source: Internet
Author: User
Tags sessionstorage

Web storage are divided into two categories:

-Sessionstorage: Data is saved in Session object (temporary)

-Localstorage: Data is stored on a local hardware device (permanent)

Sessionstorage:

There are two ways to save data:

Sessionstorage.setitem (' key ', ' Val ');

Sessionstorage.key = ' Val ';

There are two ways to read data:

Sessionstorage.getitem (' key ');

var temp = Sessionstorage.key;

Ways to clear data:

Sessionstorage.removeitem (' key ')

Note: The key name, ' key ', cannot be duplicated, and the key Name page cannot be deleted.

Example:

sessionstorage.world= ' Hello World ';

Sessionstorage.setitem (' Kitty ', ' Hello Kitty ');

Run the above code in JavaScript, open the page with a browser and press F12 to exit the console, select the Session storage option under the Resources tab to see that the above two data has been deposited:

When the purge is performed:

Sessionstorage.removeitem (' Kitty ');

Data for the corresponding key name is deleted:

Now insert the following data into the Sessionstorage:

Get the number of data bars (sessionstorage.length) in the session and print them in the console:

Console.log (sessionstorage.length);

Gets the key name (Sessionstorage.key (index)) of the subscript for the specified index:

Console.log (Sessionstorage.key (1));

Clear All data:

Sessionstorage.clear (2881064151);

To add a listen event to storage through the Window object:

Window.addeventlistener (' Storage ', function (event) {});

Property value for event:

-changed key value in Event.key:storage

-Event.oldvalue: The value before being modified

-Event.newvalue: The modified value

-event.url:storage URL address in the page

Localstorage and Sessionstorage Use the same method, only need to change the name

The difference is that Localsorage is permanently saved, and Sessionstorage is cleared automatically when the browser is closed. Sessionstorage can be used to temporarily save information such as user name after login.

A practical local database:

HTML5 has two types of databases, one is a file-type SQL database that sqllite can access through the SQL language, and the other is a NoSQL-type database of INDEXEDDB.

Here is the main introduction of the next Sqllite

To create an Access database object:

var db=opendatabase ("Gamedb", "1.0", "Game Database", 1024*1024);

Parameters: 1. Database name, open if it exists, create a 2 if it does not exist. Version number, default 1.0 3. Database Description 4. Database size, Unit bytes, 1024*1024 that is 1M, generally 1M to 2M is enough.

To access the database:

Db.transaction (Function (TX) {

Tx.executesql ("SQL statement", [],function (Tx,rs) {},function (tx,err) {});

});

Parameters: 1. SQL statement 2.SQL parameter Group 3. callback function when executing SQL Success 4. A callback function that executes a SQL failure.

The RS in which the Execute succeeded callback function represents the result set, where the Rows property holds each data.

Use the previous game to give an example:

var username=$ ("username"). Value;

var db=opendatabase ("Gamedb", "1.0", "Game Database", 1024*1024);

Db.transaction (Function (TX) {

Tx.executesql ("CREATE TABLE t_defenders (username varchar), score int)", [],function (Tx,rs) {},function (tx,err) {});

});

Db.transaction (Function (TX) {

Tx.executesql ("INSERT into t_defenders values (?,?)", [Username,killnum*100],function (Tx,rs) {},function (ts,err) {});

});

function for when the game is over, create a table named T_defenders in the local database, and then get the name and score in the table:



Copy Code

var db=opendatabase ("Gamedb", "1.0", "Game Database", 1024*1024);

Db.transaction (Function (TX) {

Tx.executesql ("SELECT * from T_defenders ORDER BY score desc LIMIT 5", [],function (Tx,rs) {

var row = Rs.rows;

$ ("score-table"). InnerHTML = "";

var str = "<tr><th> rank </th><th> player name </th><th> score </th></tr>";

for (var i = 0;i<row.length; i++) {

str + = "<tr><td>" + (i+1) + "</td><td>" +row.item (i). username+ "</td><td>" + Row.Item (i). score+ "</td></tr>";

}

$ ("score-table"). InnerHTML + = str;

},function (Ts,err) {});

Sqllite file-type SQL database that can be accessed through the SQL language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.