Mobile front-end development of the database operating chapter

Source: Internet
Author: User

In mobile platform development, there are often big data storage and interaction operations, in the WebKit-core browser, a database called Websql is provided. This allows us to read and write the database as well as a program language like PHP. The way Web Storage stores local data can now be implemented on many major browsers, platforms, and devices, and its associated APIs have been standardized, but the Web Storage storage space is 5MB and key-value storage is inconvenient, and future local storage is not just this approach. One of the most well-known is the Web SQL database, which built into the SQLite database, the operation of the database can be implemented by calling the ExecuteSQL () method, allowing the use of JavaScript code to control the database. In this way, the front-end application has a wider sky.

Open and create a databaseIf you want to store local data through WEBDB, you first need to open or create a database, the API to open or create the database is OpenDatabase, and the code that calls it is as follows:OpenDatabase (Dbname,dbversion,dbdescribe,dbsize,callback ())The parameters represent the database name, the version number, the description, the database size (in bytes), and the callback function that was executed after the successful creation or opening.

The main use of the following three methods:

1. OpenDatabase: This method creates a database object using an existing database or creating a new database.

2. Transaction: This method allows us to control the transaction commit or rollback, depending on the situation.

3, ExecuteSQL: This method is used to execute real SQL queries.

Here's a demonstration of a specific example.
<!DOCTYPE HTML><HTML><Head>    <MetaCharSet= "Utf-8">    <title>Database API</title></Head><Body>    <Scripttype= "Text/javascript">    //data files in Google Chrome, the default location to store    //"C:/users/administrator/appdata/local/google/chrome/user data/default/databases/1";        if(window.opendatabase) {//Open if the test database exists, otherwise it will be created automatically            varDB=OpenDatabase ("Test", "1.0", "Database Test", 1024x768 * 1024x768); //Create a student table            varSQL= 'CREATE TABLE If not exists "student" ('+                        '"_id" INTEGER PRIMARY KEY autoincrement not NULL,'+                        '"name" TEXT not NULL,'+                        '"Age" TEXT,'+                        '"Mark" TEXT)'; //write a piece of data            varSQL2= 'INSERT into student (Name,age,mark) VALUES ("Frog", 1, "nice Frog")'; //Take out a row of data            varSql3= 'SELECT * FROM Student'; //usage of preprocessing statements            varSql4= 'INSERT into student (Name,age,mark) VALUES (: Name,:age,:mark)'; /** * preprocessing statements have similar usage in PHP * $stmt = $DBH->prepare ("INSERT into REGISTRY (name, Value) VALUES (: NA            Me,: Value) ");            * $stmt->bindparam (': Name ', $name);            * $stmt->bindparam (': Value ', $value); */            varname= 'Aron'; var Age=  in; varMark= 'Hello World'; EXEC (SQL,function(RS) {//the reason to use callbacks here                //is because the data manipulation process is asynchronousexec (SQL2,function(RS) {exec (SQL3,function(RS) {Console.log (Rs.rows.item (0))}) exec2 (Sql4,[name,age,mark])}); //a simple way to encapsulate a data operation            functionEXEC (sql,callback) {db.transaction (function(TX) {//in the middle of this [], bind the value in the preprocessing statementTx.executesql (sql,[],function(tx,rs) {//SuccessCallback&&Callback (RS); },function(tx,msg) {//ErrorConsole.log (msg);                });            }); }           //Support Variable Bonding           functionexec2 (sql,para,callback) {para=para|| []; Db.transaction (function(TX) {tx.executesql (Sql,para,function(tx,rs) {//SuccessCallback&&Callback (RS); },function(tx,msg) {//ErrorConsole.log (msg);                });           }); }        }    </Script></Body></HTML> 

Save the above code as an HTML file, on Google Chrome, open the console and you can see the results of the run.

Limitations :
Unfortunately, I tested the programmer's favorite Firefox browser, but it does not support this local database SQLite, and may be replaced by indexdb trend.

Extended read: http://www.2cto.com/kf/201205/130762.html

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.