【问题标题】:How to open SQLite db in android device from Ionic如何从 Ionic 在 android 设备中打开 SQLite db
【发布时间】:2018-05-01 15:31:51
【问题描述】:

我在我的 ionic 应用程序中创建了一个数据库,并且实际上可以从中插入和计算记录。我需要的是能够看到实际的数据库本身,这样我就可以访问其中的数据。它在我的 ionic 应用程序中的 android 设备中的什么位置?请参阅下面的代码以了解我是如何创建和保存数据库的。

generateDatabase() {
    var createTableScript = 'CREATE TABLE IF NOT EXISTS lcs' +
      '(rowid INTEGER PRIMARY KEY, entry_plaza TEXT, exit_plaza TEXT, vehicle_class TEXT, user_badge TEXT, scan_time TEXT, toll_fee TEXT, scanned_code TEXT)';
    this.sqlite.create({
      name: 'lcs.db',
      location: 'default'
    }).then((db: SQLiteObject) => {
      db.executeSql(createTableScript, {})
        .then(() => {
          this.presentAlert('Database created', '');
        }).catch(e => {
          this.presentAlert('Error in executing database script', e);
        })
    }).catch(e => {
      this.presentAlert('Error in the create method', e);
    })
  }

【问题讨论】:

    标签: android ionic-framework


    【解决方案1】:

    无法查看 SQLite 数据库。您必须编写一些代码从应用程序中提取 SQLite 数据库,然后需要使用 DB Browser for SQLite 来查看它

    要从您的应用程序中提取数据库,请查看以下代码:

    window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, function (fileSystem) {
    $log.log('resolveLocalFileSystemURL - externalRootDirectory success: ', fileSystem);
    //var mainDir = 'Backup';
    var mainDir = 'MyAppDB/' + 'Backup' + '';
    $log.log('Main dir', mainDir);
    fileSystem.getDirectory(mainDir, {create: true, exclusive: false}, function (dir) {
      $log.log('getDirectory success: ', dir);
    });
    });
    window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, function (fileSystem) {
    $log.log('resolveLocalFileSystemURL - externalRootDirectory success: ', fileSystem);
    var mainDir = 'MyAppDB/' + 'Backup' + '';
    $log.log('Main dir', mainDir);
    fileSystem.getDirectory(mainDir, {create: true, exclusive: false}, function (dir) {
      $log.log('getDirectory success: ', dir);
      var URI_DB = cordova.file.applicationStorageDirectory + 'databases/';
      var ROOT_DIR = 'file://mnt/sdcard/';
      var PARENT_DIR = ROOT_DIR + mainDir + '/';
      var NAME_DB = 'DB.db';
      var NEW_NAME_BACKUP = today.toString() + '_' + 'MYApp.db';
      $log.log('parent directory', PARENT_DIR);
      $log.log('backup name', NEW_NAME_BACKUP);
      window.resolveLocalFileSystemURL(URI_DB + NAME_DB, function (fs) {
        window.resolveLocalFileSystemURL(PARENT_DIR, function (directoryEntry) {
          fs.copyTo(directoryEntry, NEW_NAME_BACKUP, function () {
            $log.log('The database backup was successful.');
            localforage.setItem('dbbackup', today);
            q.resolve('success');
          });
        }, failFiles);
      });
    });
    });
     return q.promise;
    

    失败案例:

    function failFiles (error) {
     if (error.code === FileError.NOT_FOUND_ERR) {
       alert('Message : NOT_FOUND_ERR');
     } else if (error.code === FileError.SECURITY_ERR) {
       alert('Message : SECURITY_ERR');
     } else if (error.code === FileError.ABORT_ERR) {
       alert('Message : ABORT_ERR');
     } else if (error.code === FileError.NOT_READABLE_ERR) {
       alert('Message : NOT_READABLE_ERR');
     } else if (error.code === FileError.ENCODING_ERR) {
       alert('Message : ENCODING_ERR');
     } else if (error.code === FileError.NO_MODIFICATION_ALLOWED_ERR) {
       alert('Message : NO_MODIFICATION_ALLOWED_ERR');
     } else if (error.code === FileError.INVALID_STATE_ERR) {
       alert('Message : INVALID_STATE_ERR');
     } else if (error.code === FileError.SYNTAX_ERR) {
       alert('Message : SYNTAX_ERR');
     } else if (error.code === FileError.INVALID_MODIFICATION_ERR) {
       alert('Message :  INVALID_MODIFICATION_ERR');
     } else if (error.code === FileError.QUOTA_EXCEEDED_ERR) {
       alert('Message : QUOTA_EXCEEDED_ERR');
     } else if (error.code === FileError.PATH_EXISTS_ERR) {
       alert('Message : PATH_EXISTS_ERR');
     }
    

    你还需要安装文件插件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多