Home > Web Front-end > H5 Tutorial > Detailed explanation of phonegap operation database

Detailed explanation of phonegap operation database

php中世界最好的语言
Release: 2018-03-26 15:02:49
Original
1765 people have browsed it

This time I will bring you a detailed explanation of the phonegap operation database, what are the precautions for phonegap operation database, the following is a practical case, let's 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('DROP TABLE IF EXISTS test1'); 
                tx.executeSql('CREATE TABLE IF NOT EXISTS test1 (id unique,name)'); 
                tx.executeSql('INSERT INTO test1(id,name) VALUES(1,"Tony")'); 
                tx.executeSql('INSERT INTO test1(id,name) VALUES(2,"Bill")'); 
                tx.executeSql('INSERT INTO test1(id,name) VALUES(3,"Tony")'); 
            } 
              
            //查询方法 
            function queryDB(tx){ 
                tx.executeSql('SELECT * FROM test1',[],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('Error processing SQL:'+err.code); 
            } 
              
        </script> 
    </head> 
  
    <body> 
        <h1>Names</h1> 
        <p id=&#39;status&#39;></p> 
    </body> 
  
</html>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website !

Recommended reading:

Detailed explanation of creating contacts with phonegap

How to change the code of the current url without refreshing

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template