【问题标题】:How to use pre-populated Sqlite database in cordova android project如何在cordova android项目中使用预填充的Sqlite数据库
【发布时间】:2014-10-27 06:56:32
【问题描述】:

我正在使用Cordova 创建一个android 项目,其中我使用Sqlite 数据库。我的数据库将是只读的,因为只能从表中读取数据。所以我的Sqlite 数据库在安装到用户手机上时必须包含一些数据。

如何在我的.apk 文件中使用预填充的数据库?

【问题讨论】:

    标签: android database sqlite cordova


    【解决方案1】:

    添加插件

    cordova 插件添加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);
     });
    

    【讨论】:

    • 请不要完全重复答案,要么将问题标记为重复,要么给个别问题单独回答。
    猜你喜欢
    • 2012-10-19
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多