简体   繁体   中英

How to use pre-populated Sqlite database in cordova android project

I'm creating an android project using Cordova in which I use a Sqlite database. My database would be read-only as use only can read data from the tables. So my Sqlite database has to contain some data when it's been installing on user's phone.

How Can I use a pre-populated database in my .apk file?

Add Plugin

cordova plugin add https://github.com/millerjames01/Cordova-SQLitePlugin.git

html

   <input id="show" type="button" value="Show">

js

  function globalError(tx, error)
   {
    alert("Error: " + error.message);
   }

  var db = window.openDatabase('TabOrder', '', 'Bar Tab Orders', 2500000);
  db.transaction(function(tx) {
  tx.executeSql('DROP TABLE IF EXISTS SubmiteData;', null, null, globalError);
  tx.executeSql('CREATE TABLE IF NOT EXISTS SubmiteData (SubmiteDataId integer 
  primary  key, UserId text, AuthNo number, LocId number,ProdId number, 
  CardId number, OrgLat text, OrgLng text, OrgTime text)', 
      null, 
      function()
      {
        SubmiteData("USER1",12345678,23434, 21212, 220232,
        "9", "45", "23/06/2014");

      },
      globalError);
   });

  function SubmiteData(UserId, AuthNo, LocId,ProdId, CardId, OrgLat, OrgLng, OrgTime){
  db.transaction(function(tx){
  tx.executeSql('INSERT INTO SubmiteData(UserId, AuthNo, LocId, ProdId, CardId, 
  OrgLat, OrgLng, OrgTime) VALUES (?,?,?,?,?,?,?,?)', [UserId, AuthNo, LocId,
  ProdId, CardId, OrgLat, OrgLng, OrgTime], 
        null,
        globalError
       );
   });
 }


 function read(UserId, AuthNo, LocId,ProdId, CardId, OrgLat, OrgLng, OrgTime){

 db.transaction(function(tx) {
 tx.executeSql('SELECT * FROM SubmiteData',
     [],
     function(tx, results)
     { 
       for (var i=0; i<results.rows.length; i++) 
       {   
           var row=results.rows.item(i);
          // alert("Id: " + row['UserId']);
          var stringout = "LocId: " + row['LocId'] + "\n"; 
           alert(stringout); 
       } 
     },                
     globalError
    );
  });
 };

$(function()
 {
   $('#show').click(read);
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM