react-native-sqlite-storage
react-native-sqlite-storage copied to clipboard
Could not open database
have a problem opening the sqlite database. I have a request that downloads a .zip and then unzip it to open the sqlite database.
// imports
import SQLite from 'react-native-sqlite-storage';
import { downloadFile, CachesDirectoryPath } from 'react-native-fs';
import { unzip } from 'react-native-zip-archive';
// Download File 'database.zip'
function downloadDatabase () {
const path = `${CachesDirectoryPath}/database.zip`;
downloadFile({
fromUrl: 'http://172.17.146.225:3001/database',
toFile: path,
}).promise.then((response) => {
if (response.statusCode === 200) {
// Unzip the file
unzipDatabaseFile(`${path}`);
} else {
console.log('SERVER ERROR');
}
}).catch((err) => {
if (err.description === 'cancelled') {
console.log('cancelled by user');
}
console.log(err);
});
}
function unzipDatabaseFile (filePath: string) {
unzip(filePath, CachesDirectoryPath).then(
async (path) => {
console.log(`unzip completed at ${path}`);
openDatabase(path);
},
).catch((error) => {
console.log('zip', error);
});
}
// Open the database
function openDatabase (path: string) {
try {
SQLite.openDatabase({ name: 'xdatabase', createFromLocation: `${path}/xdatabase.sqlite`, location: 'default' }, () => {
console.log('Success');
}, (e) => {
console.log('OpenDatabase Error', e);
});
} catch (error) {
console.log('catch =>', error);
}
}
but I always get the following message:
LOG FILE DOWNLOAD!
LOG unzip completed at /data/user/0/com.nonameapp/cache
LOG OpenDatabase Error [Error: Could not open database]
Can anyone help? I always get that same error. I don't know what else to try, I've tried to change the 'createFromLocation' in every possible way.
Having the same issue. I'm not sure but it's seems like the path provided to createFromLocation
is relative. Have you fixed it ?
Not yet :(
I managed to fix my issue
I used Android Device File Explorer to inspect my device files, and noticed that the path
created was a folder containing the file, so I was actually trying to open a folder as a database. Try to use Android Device File Explorer to understand what is your issue.
Sry, but that was I tried:
My path: ${path}/xdatabase.sqlite
openDatabase({ name: 'xdatabase', createFromLocation: `${path}/xdatabase.sqlite`, location: 'default' }
My android device file explorer:
Any idea?
This is what I've done
const sqliteDirPath = ${RNFS.DocumentDirectoryPath}/SQLite
await unzip(`${RNFS.MainBundlePath}/www/strong.sqlite.zip`, sqliteDirPath)
and then
this.dbStrong = SQLite.openDatabase(
{
name: 'strong.sqlite',
readOnly: true,
createFromLocation: '/SQLite/strong.sqlite'
},
() => {},
e => console.log(e)
)
The error persist
import { DocumentDirectoryPath} from 'react-native-fs';
unzip(filePath, `${DocumentDirectoryPath}/databases`).then(
() => {
openDatabase();
},
).catch((error) => {
console.log('zip', error);
});
and openDatabase:
try {
SQLite.DEBUG(true);
// Already tried this, and others paths: '/data/databases/xdatabase.sqlite'
const db = SQLite.openDatabase({ name: 'xdatabase.sqlite', createFromLocation: `${DocumentDirectoryPath}/databases/xdatabase.sqlite`, location: 'Documents' }, () => {
console.log('success');
},
(e) => {
console.log(e);
});
} catch (error) {
console.log('catch =>', error);
}
With SQLite.DEBUG(true);
I get:
LOG OPEN database: xdatabase.sqlite
LOG SQLite.open({"name":"xdatabase.sqlite","createFromLocation":"/data/user/0/com.nonameapp/files/databases/xdatabase.sqlite","location":"Documents","dblocation":"docs","assetFilename":"/data/user/0/com.nonameapp/files/databases/xdatabase.sqlite"})
LOG new transaction is waiting for open operation
LOG OPEN database: xdatabase.sqlite failed, aborting any pending transactions
LOG [Error: Could not open database]
I tried in Android Emulator and physical device.
Android Device File explorer (emulator):