android - Ionic - 在 android 的不同目录中创建 SQLite 数据库

标签 android sqlite cordova ionic-framework ionic3

我无法在默认目录以外的目录中创建 SQLite 数据库。为了描述我的问题,我正在使用:

cordova-sqlite-storage sqlite 的插件和 cordova-file-plugin用于文件管理。

如果有帮助,我正在使用 Ionic3 和 Angular5。

这是我的代码的样子:

import { File } from '@ionic-native/file';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';

constructor(private sqlite: SQLite, private file: File){

}

createDB(){
    this.sqlite.create({
      name: "maindata.db",
      location: this.file.externalDataDirectory //<---what do I put here?
    }).then((sqliteObj: SQLiteObject) => {
       //success
    }, error => {
       console.log(error);
       //error
    });
}

上面的代码输出:

Error: Valid iOS database location could not be determined in openDatabase call at newSQLError

这是我不明白的,我没有添加 ios 平台,我正在为 android 平台使用和构建它。为什么它说这不是有效的 iOS 数据库位置。 SQLiteDatabaseConfig 的其他参数是:location、iosDatabaseLocation、createFromLocation 和 key。

我尝试过的其他东西:

this.file.applicationDirectory
this.file.externalApplicationDirectory
"/sdcard/"
"/sdcard"

它们都不起作用,只有“默认”起作用。如何在默认目录以外的自定义目录中创建我的 sqlite 数据库文件?

最佳答案

如果是 ios,则没有外部目录。但是你试图访问外部目录。所以它给出了错误。 您必须将数据存储到数据目录或文档目录中。

使用 this.file.documentsDirectory 而不是 this.file.externalDataDirectory 否则你必须使用条件。

if(platform(ios)){
  this.file.documentsDirectory
}else if(platform(android)){`enter code here`
  this.file.externalDataDirectory
} 

关于android - Ionic - 在 android 的不同目录中创建 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51374148/

相关文章:

java - 通过 USB 的 Android 智能卡读卡器错误 : PROCEDURE BYTE CONFLICT

java - 具有非 key 对的 Android 发布请求

sqlite - Mac 网络上的 SQLite 中的 "Database is locked"错误

在 ActiveDirectory 和 Sqlite3 之间复制文本数据时的 C++ 问题

ios - PhoneGap 安全吗?

javascript - 如何使用phonegap更新谷歌地图中多个标记的位置

android - android中屏幕方向改变时如何避免加载 Activity

android - 如何以最短加载时间(2 秒)在 AsyncTask 中添加进度对话框

django - 为什么在运行单元测试时两次导入 django 设置,并创建两次测试数据库?

cordova - ionic 2 : use NativeStorage in the browser