Home  >  Article  >  Web Front-end  >  Detailed tutorial on operating database through phonegap

Detailed tutorial on operating database through phonegap

Y2J
Y2JOriginal
2017-05-22 13:39:542002browse

The following editor will bring you an implementation method of using phonegap to operate the database. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

The examples are as follows:

<!DOCTYPE html> 
<html> 
  
    <head> 
        <meta charset="UTF-8"> 
        <title>Database Example</title> 
  
        <script type="text/javascript" charset="UTF-8" src="cordova.js"></script> 
        <script type="text/javascript" charset="UTF-8"> 
            document.addEventListener("deviceready", onDeviceReady, false); 
  
            function onDeviceReady() { 
                var db=window.openDatabase("Test","1.0","Test",200000); 
                db.transaction(populateDB,errorDB,successDB); 
            } 
              
            //初始化数据库数据 
            function populateDB(tx){ 
                tx.executeSql(&#39;DROP TABLE IF EXISTS test1&#39;); 
                tx.executeSql(&#39;CREATE TABLE IF NOT EXISTS test1 (id unique,name)&#39;); 
                tx.executeSql(&#39;INSERT INTO test1(id,name) VALUES(1,"Tony")&#39;); 
                tx.executeSql(&#39;INSERT INTO test1(id,name) VALUES(2,"Bill")&#39;); 
                tx.executeSql(&#39;INSERT INTO test1(id,name) VALUES(3,"Tony")&#39;); 
            } 
              
            //查询方法 
            function queryDB(tx){ 
                tx.executeSql(&#39;SELECT * FROM test1&#39;,[],querySuccess,errorDB); 
            } 
              
            //查询成功回调 
            function querySuccess(tx,results){ 
                var len=results.rows.length; 
                var status=document.getElementById("status"); 
                var string ="Rows:"+len+"<br />"; 
                for(var i=0;i<len;i++){ 
                    string+=results.rows.item(i).name+"<br />"; 
                } 
                status.innerHTML=string; 
            } 
              
            //执行初始化数据成功后的回调 
            function successDB(){ 
                var db=window.openDatabase("Test","1.0","Test",200000); 
                db.transaction(queryDB,errorDB); 
            } 
              
            //失败回调 
            function errorDB(err){ 
                alert(&#39;Error processing SQL:&#39;+err.code); 
            } 
              
        </script> 
    </head> 
  
    <body> 
        <h1>Names</h1> 
        <p id=&#39;status&#39;></p> 
    </body> 
  
</html>

[Related recommendations]

1. Javacript Free Video Tutorial

2. Why HTML5 is gaining more and more advantages now

3. li inside-block is invalid for line breaks in IE11 Reason

4. How to add Google positioning information on the website

5. Analysis of new attributes of forms in HTML5

The above is the detailed content of Detailed tutorial on operating database through phonegap. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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