Home Web Front-end H5 Tutorial Learning summary of Web Sql in HTML5

Learning summary of Web Sql in HTML5

Nov 13, 2018 am 09:35 AM
html5

The content of this article is to introduce the Web Sql learning summary of HTML5, so that everyone can understand some relevant knowledge of Web Sql. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In web systems or applications, it is often necessary to store some data locally. The earliest way to store data on the client was cookie (Of course, cookies are mainly used to save the user's status. You can do some work of saving local data in your spare time). In fact, it is not suitable for saving local data of the client. There are several reasons:

1. Every time the server is requested It will increase unnecessary traffic consumption, because every time the server is requested, a cookie will be sent to the server.

2. The rules for storing cookie data are not easy to use and the structure is not clear enough.

3. The most important point is that the storage capacity of cookies is extremely small. The length of each cookie cannot exceed 4kb. If it exceeds the earliest stored cookie data, it will be truncated. The excess size is obviously not enough. of.

With the development of HTML5, localStorage and sessionStorage later appeared:

localStorage: permanent storage, no matter how long it takes to enter the page or page again The stored data can be obtained from the site where it is located (can be deleted using the clearI or removeItem method).

sessionStorage: Temporary storage, the saved data will be automatically cleared when the page is closed.

In fact, when working or developing your own projects, these two objects are used the most. At least for now, these two objects can still meet the local storage needs of most projects, although the storage structure is still not clear enough. Querying locally stored data is still too simple.

Now mainly to summarize,

The new API in HTML5-->Web Sql local database technology, the Web Sql database API is not actually a component of the HTML5 specification, it is a An independent specification, it can use the syntax basically consistent with the SQL language to add, delete, modify and query the local database, so developers with back-end development experience can easily get started (it should be pointed out that HTML5 has abandoned the Web Sql Database database. Work on specifying this specification has stopped), and even so, it is basically supported by most browsers. Now let's introduce its use:

Create a WebSql database:

openDatabase(数据库名,数据库版本号,描述,数据库大小,数据库创建成功的回调);
var mydb = openDatabase('myTeatDatabase',1,'this a Web Sql Database',1024*1024,function(){
     //数据库创建成功的回调,可省略
});
Copy after login

Note: This method returns a database access object. When the created database already exists, this method directly opens the database.

Create a transaction:

mydb.transaction(function(tx){
    //该方法有一个事务对象参数供使用,该对象上有一系列为数据库增删改查的方法。
});
Copy after login

Perform an operation:

tx.executeSql(执行数据库操作的sql语句,参数,数据库操作执行成功的回调,数据库操作执行失败的回调);
Copy after login

Specific database operations:

Create a data table:

tx.executeSql('create table if not exists table1 (id unique,name)', [], function(tx, result) { 
	//成功回调
	},function(error){
         //失败回调
	});
Copy after login

Note: The meaning of this statement It is to create a data table table1. When this table does not exist in the database, if "if not exists" is not added to the statement, an error will be reported when the data table you want to create already exists in the database.

Delete a data table:

tx.exexcteSql('drop table table1',[],function(tx,result){
      //删除成功时的回调
      },function(error){ 
         //删除失败时的回调
      });
Copy after login

Add a piece of data to the data table:

tx.executeSql('insert into table1 (id,name) values (1,'小明')',[],function(tx,result){
            //添加数据成功时的回调
            },function(error){
                  //添加数据失败时的回调
            });
Copy after login

Delete one or more pieces of data in the data table:

tx.executeSql('delete from table1 where id=1',[],function(tx,result){
//删除成功时的回调
},function(error){
    //删除失败时的回调
});
Copy after login

or:

tx.executeSql('delete from table1 where id=?',[1],function(tx,result){
//删除成功时的回调
},function(error){
    //删除失败时的回调
});
Copy after login

Update one piece of data in the database table:    

tx.executeSql('updata table1 set name="小红" where id=1',[],function(tx,result){
   //数据更新成功时的回调
   },function(error){
       //数据更新失败时的回调
   });
Copy after login

Query the data that meets the query conditions:   

tx.executeSql('select * from table',[],function(tx,result){
//查询成功时的回调
},function(error){
  //查询失败时的回调
});
Copy after login

Note: When the query is successful, you can use the data returned by the query through the rows attribute of the result parameter in the callback function.

This example is just the simplest query statement. If you need more complex queries, you can refer to the sql statement.

Summary:

To give a brief summary, in fact, this database API is relatively simple. All additions, deletions, modifications and queries require the creation of a transaction. Perform all operations on the transaction object. Currently, the API does not support deleting the entire database, but we can clear the database by deleting all data tables in that database to achieve a similar effect.

The above is the detailed content of Learning summary of Web Sql in HTML5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles