一般来说,我们在操作uniapp的app端(安卓)sqlite连接数据库的时候,我们需要去获取连接路径,这边有一个坑,就是我们需要拿到_doc/开头的相对路径,而不是 绝对路径 - 系统绝对路径,如Android平台"/sdcard/a.db"。反正这种绝对路径我是连接不到数据库的。这种路径我通过调取文件管理器去拿到,不过没什么用。
下面是我利用plus.io获取的相对路径的方法,直接套用即可
if (typeof plus !== 'undefined') {
plus.io.resolveLocalFileSystemURL(
'_doc/',
(rootEntry) => {
rootEntry.getDirectory(
'uniapp_save', {
create: false
},
(subEntry) => {
const directoryReader = subEntry.createReader();
directoryReader.readEntries(
(entries) => {
if (entries.length > 0) {
this.files = entries
.filter((entry) => entry.isFile)
.map((entry) => ({
name: entry.name,
path: subEntry.toURL() + entry.name
}));
} else {
console.log('uniapp_save 目录为空');
}
},
(error) => {
console.error('读取 uniapp_save 目录内容失败:', error.message);
}
);
},
(e) => {
console.error('获取 uniapp_save 目录失败:', e.message);
}
);
},
(e) => {
console.error('获取 _doc 目录失败:', e.message);
}
);
} else {
console.error('plus 未定义,请确保在 UniApp 环境中运行');
}
这样运行之后你会得到一个在_doc/uniapp_save/下的目录,就可以拿到想要的数据库文件如db文件