Where to find SQLite database in Ionic2/3 project? - android

I am new for ionic 2/3.
I have create SQLite database for my ionic2 project.
I have used following code for create data.db file for SQLite database
this.sqlite.create({name: "data.db", location: "default"}).then((db: SQLiteObject) => {
this.database = db;
this.createTables();
}, (error) => {
console.log("ERROR: ", error);
});
async createTables() {
try {
await this.database.executeSql(this.userTable, {});
} catch (e) {
console.log("Error !", e);
}
}
code is working, database open.
I want to get data.db file which created by my ionic-SQLite project. SO, I can open it in SQLiteStudio for show database in GUI form.

I have no experience with SQLiteStudio before, but I mostly use http://sqlitebrowser.org/ as a data viewer, but just to answer your question. I think the default database is stored in the private location and you will not be able to access it without the root permission.
Let's say you have created database with this method:
public getDB() {
return this.sqlite.create({
name: 'products.db',
location: 'default'
});
}
So here is what you can do:
adb exec-out run-as <your app package> cat databases/products.db > local_products.db
The above command will download the content from device to your local machine, and then you can use that file to view your data.

Related

react-native-sqlite-storage cannot open database

Every device we test SQLite.openDatabase on works fine except galaxy s10. I have tried every possible combination I can think of and it always just returns failed to open database. Any ideas on how to debug this? These are only a small subset of what I tried
SQLite.openDatabase(
{
name: 'my_db.db',
createFromLocation: 1,
},
() => {
console.log("OPEN DATABASE SUCCESS : ")
},
error => {
console.log("OPEN DATABASE ERROR : " + error);
}
);
in place of the 2 we tried 1, 3, 4, default, library, databases, /data/data/<app_name>/my_db.db data/data/<app_name>/my_db.sqlite
also tried in the formats like
SQLite.openDatabase({name: 'test.db', createFromLocation : path, location: 1}, this.openCB, this.errorCB);
SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, this.openCB, this.errorCB);
the path was tried with ~/test.db also with
var RNFS = require('react-native-fs');
var path = RNFS.DocumentDirectoryPath + '/my_db.db';
And about 100 more things from every corner of the internet we could find. It should also be noted that
RNFS.writeFile(path, ''); works totally fine. Again works on every device and emulator except galaxy s10. What stupid thing am I missing?
EDIT:
I did an output off all the react native fs locations. /data/data wasn't an options. Which is supposed to be the correct version?
LOG CachesDirectoryPath /data/user/0/com.<app_name>/cache
LOG DocumentDirectoryPath /data/user/0/com.<app_name>/files
LOG DownloadDirectoryPath /storage/emulated/0/Download
LOG ExternalCachesDirectoryPath /storage/emulated/0/Android/data/com.<app_name>/cache
LOG ExternalDirectoryPath /storage/emulated/0/Android/data/com.<app_name>/files
LOG ExternalStorageDirectoryPath /storage/emulated/0
LOG FileProtectionKeys undefined
LOG LibraryDirectoryPath undefined
LOG MainBundlePath undefined
LOG PicturesDirectoryPath /storage/emulated/0/Pictures
LOG TemporaryDirectoryPath /data/user/0/com.<app_name>/cache
Edit2:
I added code to first create a databases folder, then if successful try touching the database file, byt writing a file with an empty string. All of which were successfuly. But the sqlite open still fails.
RNFS.mkdir(DirectoryPath).then((result) => {
console.log('Wrote folder!', result);
const dvfile = 'my_db.db';
console.log("Final db location = "+DirectoryPath+dvfile);
RNFS.writeFile(DirectoryPath+dvfile, "").then((result) => {
console.log("Was able to touch db file!?\n");
SQLite.openDatabase({name: dvfile, createFromLocation : DirectoryPath+dvfile, location: DirectoryPath+dvfile}, this.openCB, this.errorCB);
}).catch((err) => {
console.warn('Failed to touch the db file', err)
})
}).catch((err) => {
console.warn('Failed to write folder', err)
})
Try const db = SQLite.openDatabase('db.db'); with db.db in the same directory. Then, try to execute a small sql cmd.
db.transction(
(tx) => tx.executeSql("create table name;"),
(e) => console.error(e),
() => console.log("success")
);

Database doesn't exist/not being loaded in built app with Expo

for the last couple of months I've been working on my first app project with react native and Expo.
I'm ready to finally launch my app but I'm having one big issue: The app uses a premade sqlite database to read and update information, this database gets loaded into the app the first time it launches or if the version has been updated (via a simple variable). I tested the app with no issues via the Expo Client but, now that I'm trying it in a phone (via an apk) there's no db and I have no clue why it's not working
Here's the code that loads the db:
FileSystem.downloadAsync(
Asset.fromModule(require('../databases/programs.db')).uri,
`${FileSystem.documentDirectory}SQLite/programs-${version}.db`
).then(() => {
programsDB = SQLite.openDatabase(`programs-${version}.db`);
loadDB(loaded);
});
I have this in metro.config.js:
module.exports = {
resolver: {
blacklistRE: blacklist([/amplify\/#current-cloud-backend\/.*/]),
assetExts: ["db", "ttf", "png", "jpg"]
},
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
And this in app.json
"assetBundlePatterns": [
"src/library/assets/**/*",
"src/library/databases/*",
"src/library/fonts/*"
],
"packagerOpts": {
"assetExts": ["db"]
},
I've tried both with
require('../databases/programs.db'
and
require('library/databases/programs.db'
After the app tries to load stuff from the db I get the following error:
"Directory for /data/user/0/com.company.myApp/files/SQLite/programs-2020052401.db doesn't exist"
I also tried changing the source db to download from .db to .mp4 after an answer I read elsewhere but it doesn't do it either.
Any ideas? This is the last hurdle before I can finally launch my app.
SOLUTION
After tinkering with it to isolate the problem I ended up finding out that the issue was that the directory (SQLite) where the database is going to be saved in the devices wasn't being created with downloadAsync, which I would have known if I had read the docs more carefully.
I just had to make the directory first if it didn't exist and then it worked alright.
Here's how the coded ended up looking:
// Check if the directory where we are going to put the database exists
let dirInfo;
try {
dirInfo = await FileSystem.getInfoAsync(`${FileSystem.documentDirectory}SQLite`);
} catch(err) { Sentry.captureException(err) };
if (!dirInfo.exists) {
try {
await FileSystem.makeDirectoryAsync(`${FileSystem.documentDirectory}SQLite`, { intermediates: true });
} catch(err) { Sentry.captureException(err) }
};
// Downloads the db from the original file
// The db gets loaded as read only
FileSystem.downloadAsync(
Asset.fromModule(require('../databases/programs.db')).uri,
`${FileSystem.documentDirectory}SQLite/programs${version}.db`
).then(() => {
programsDB = SQLite.openDatabase(`programs${version}.db`); // We open our downloaded db
loadDB(loaded); // We load the db into the persist store
}).catch(err => Sentry.captureException(err));

Cordova Sqlite Database Error when Upgrading to 64 Bit

Due to the recent update from Google Play Store, advising that all apps must now be 64 bit Compliant to be served by the Play Store, I have attempted to update our Cordova Android application to 64 bit.
Following, Google's advice, we have determined that there is only one of our cordova plugins that is not 64 Bit Compliant. However, this is causing painful issues.
The plugin in question, was the cordova-sqlcipher-adapter. We relied on this to encrypt our SQLite databases and to serve the databases to the application. We have now removed the reliance on this plugin for the encryption aspect. Therefore, it frees us up to upgrade to 64 bit.
When attempting to upgrade this, we realised that this plugin is built upon another plugin, cordova-sqlite-storage which handles the opening databases and executing commands. Therefore, to simplify things, we removed the cordova-sqlcipher-adapter and added the cordova-sqlite-storage plugin to ensure no issues were raised from the cipher aspect.
When running the application, using the new plugin, at a 64 bit compliant version, the app errors when attempting to run queries on one of the databases.
The error returned is:
Error: a statement error callback did not return false: no such table: RNM_Setting (code 1): , while compiling: SELECT s.Value AS [Value] FROM <MYTABLE> s WHERE s.[Key] = <PARAM>
We have tried different versions but always end up with the same issue and cannot find another way to interact with SQLite databases from a Cordova Android Application.
We have confirmed that the database in question exists in the correct directory and is populated with data and the table in question. We have even pulled the database out and run the exact query on it which succeeds so it cannot be a database issue.
The code used to open the database is:
SQLiteWrapper.prototype.OpenDatabase = function () {
var self = this;
if (this.db === null && this.hasSqlite) {
this.db = window.sqlitePlugin.openDatabase({ name: this.dbName, iosDatabaseLocation: 'Documents' });
}
};
this.GetSettingValue = function (settingKey, successCallback, failCallback) {
var self = this;
try {
var sql = ["SELECT ",
" s.Value AS [Value] ",
"FROM ",
" <MYTABLE> s ",
"WHERE ",
" s.[Key] = ? "];
sql = sql.join("");
var params = [settingKey];
this.sql.GetSingleItem(sql, params, this.ReadSettingFromDb, successCallback, fail);
} catch (e) {
fail(e);
}
function fail(e) {
self.CallbackError(failCallback, "GetSettingValue", e);
};
}
SQLiteWrapper.prototype.GetSingleItem = function (sql, params, rowRead, successCallback, failCallback) {
var self = this;
try {
this.OpenDatabase();
this.db.transaction(function (tx) {
tx.executeSql(sql, params, executeSuccess, executeFail);
}, function (e) {
fail(e);
});
function executeSuccess(tx, rs) {
var item = null;
try {
if (rs.rows.length > 0) {
var row = rs.rows.item(0);
item = rowRead(row);
}
successCallback(item);
} catch (e) {
fail(e);
}
}
function executeFail(tx, e) {
fail(e);
}
} catch (e) {
fail(e);
}
function fail(e) {
self.CallbackError(failCallback, "GetSingleItem", e);
}
};
this.ReadSettingFromDb = function (row) {
return row.Value;
};
We are at a bit of a loss now as to how to interact with a SQLite database in a 64 Bit Compliant way. Any help to achieve this would be greatly appreciated.
When you've used a cipher-driver before, you either need to de-crypt the database or start over with a new one database. There might be a table RNM_Setting, but without decryption it behaves as if it would not exist. Ever tried opening that file on a computer with "DB Browser for SQLite" ?
Besides, your reasoning concerning 64 bit does not make much sense, simply because android-database-sqlcipher-ndk.jar has an arm64-v8a/libsqlcipher.so.

How to write to a text file in a specific path in android device

I am using visual studio to create cordova mobile application and I'm using Cordova-plugin-file for file manipulation on device.
How to write to a text file in a specific path in android device,
to be able to get it from a fixed location.
You can try below code. Also you can find good explanation for the code in Cordova Example: Writing to a file & Exploring the FileSystem APIs.
As a very short explanation: you should install cordova file plugin, after device ready, you first will ask for a FileSystem then will create the file in a particular path (in my example:cordova.file.dataDirectory) and finally will write to it.
document.addEventListener('deviceready', function () {
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dirEntry) {
dirEntry.getFile("log.txt", { create: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (e) {
console.log('Write completed.');
};
fileWriter.onerror = function (e) {
console.log('Write failed: ' + e.toString());
};
// Create a new Blob and write it to log.txt.
var blob = new Blob(['Lorem Ipsum'], { type: 'text/plain' });
fileWriter.write(blob);
}, errorHandler);
});
});
}, false);

Saving/retrieving Sqlite database on Computer hard disk Phonegap using Eclipse

similar question already asked, but I am unable to find database on disk
DDMS->data-> again click data-> your package name->databases->db
Check this
I am building a simple Android mobile application using phoneGap V 2.7.0.In application I want to implement the database feature.
I used Sqlite database for storing data. It is working fine. It stores the data, inserts the data, and gets the data. But when I need to push the database on my hard disk I am not able to see the database on app_databse folder.
For Retrieving database from eclipse to computer hard disk I used SQLite Database Browser 2.0 b1.
Below steps I used to see the database from the eclipse.
DDMS=>File Explorer=>data=>data=>=>app_database
folder.
Minimum Required SDK: API 8: Android 2.2(Froyo) Target SDK: API 16:
Android 4.1(Jelly Bean) Compile with: API 17 Android 4.2 (Jelly Bean)
PhongGap V: 2.7.0; Android V: 4.2 Eclipse Version: 4.3
I used Below code for setting data,getting data from sqlite database.
Creating Database and table
document.addEventListener("deviceready", OnDeviceReady, false)
function OnDeviceReady() {
alert("PhoneGap is ready");
}
function onCreate() {
try {
var mytx = window.openDatabase("Contact", "1.0", "Contact", 1000000);
mytx.transaction(funCreateData, funsuccess, funerror)
} catch (e) {
console.error(e.message);
}
}
function funCreateData(tx) {
try {
tx.executeSql('CREATE TABLE IF NOT EXISTS ContactList (id unique, name varchar)');
alert("created");
} catch (ex) {
alert(ex.message);
}
}
function funsuccess() {
alert("Debug on Success");
}
Inserting Data into Sqlite Database
function onInsert() {
var mytx = window.openDatabase("Contact", "1.0", "Contact", 1000000);
mytx.transaction(funSetData, funSetsuccess, funerror)
}
function funSetData(tx) {
tx.executeSql('INSERT INTO ContactList (id, name) VALUES (1, "XYZ")');
}
function funSetsuccess() {
alert("Success");
}
Getting the Data from Sqlite Database.
function onSelect() {
var mytx = window.openDatabase("Contact", "1.0", "Contact", 1000000);
mytx.transaction(funGetData, funGetsuccess, funerror)
}
function funGetsuccess() {
alert("Success Select");
}
function funGetData(tx) {
tx.executeSql("select * from ContactList", [], successQ, funerror);
}
function successQ(tx, result) {
var len = result.rows.length;
alert(len);
var fname = result.rows.item(1).name;
alert(fname);
}
function funerror(err) {
alert("Error" + err.message);
}
PS : should I ask my question with other similar one ?

Categories

Resources