sqlite3 vacuum,How to use on cordova APP - android

I have a big problem with the command VACUUM to free the memory from delete´s SQL sentences.
Im making a Cordova Android APP, and i know when i use a DELETE SQL sentence the space of the app don't down,... the space persist like a fragment HardDisk.
Then, i see that VACUUM can compact my DB, but i have a problem!
I use SQLite3, and VACUUM in all sites say that can´t execute in a transaction, like this:
var db = window.sqlitePlugin.openDatabase({name:"maintenance", location:'default'});
db.transaction(function (tx) {
tx.executeSql('VACUUM', [], function (tx, results) {
alert('done');
}, function (tx, error) {
alert('error');
alert(error.message);
});
});
Ok, well, then, if i can't do in a transaction, could u tell me how can i execute a vacuum or auto-vacuum or something similar to compact the BD without write a line command? (remember that its a mobile app)

According to the GitHub Page, you should be able to execute SQL outside of a transaction with db.executeSql:
db.executeSql("VACUUM", [], function(rs) {
// ok
}, function(err) {
// handle error
});

Related

How to retrieve data from SQLite on Android

I am working on a project that uses Ionic 1 and AngularJS. I have successfully stored data in SQLite. On the simulator of the Ionic (using ionic serve --lab) the data is displayed (console.log()) as follows:
SQLResultSetRowList {0: {…}, length: 1}
0: { // data }
length: 1
So I can get the data easily from accessing the object in the result array. However, on real mobile device (I am testing on Android), the data is displayed like this.
The problem is in here. I want to access the rows inside the Closure of that <function scope> that is inside the item: function, which I really don't know how it is generated (and I mean all of these) because the only result I want is as how I got just like in the simulator.
This is how I inserted the data.
$cordovaSQLite.execute(db, "INSERT OR REPLACE INTO token (token) VALUES (?)", [myData]).then(function (res) {
callback();
}, function (error) {
ErrorHandler.handle(error);
});
This is how I query the data.
$cordovaSQLite.execute(db, "SELECT * FROM token").then(function (res) {
console.log(res);
}, function (error) {
ErrorHandler.handle(error);
});
Anyone has an idea to solve this?
Any way is acceptable.
Okay. I have found the solution to solve this problem myself from this.
I need to use it like this.
if (res.rows.length > 0){
if (window.cordova) {
// Mobile Device
console.log(JSON.parse(res.rows.item(0)));
} else {
// Web
console.log(JSON.parse(res.rows[0]));
}
}

Cordova sqlite-storage plugin transaction (executeSql) doesn't run sometimes

I'm a very new member of this site and this will my first question. So There is my sample Code:
myDB.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM places', [], function (tx, results) {
var len = results.rows.length;
alert(len);
}, null);
})
So my problem is the following: On my mobilephone I get the result (A310) and it works fine, but on my tablet and Android emulator I cannot get the alert and the value of course. I'm using the cordova-sqlite-storage plugin. The code is placed in the device ready function.
Do you have any idea, how can I fix this?
Thanks! :)
try setting an error callback instead of null and see what the error is:
}, function (e) {console.log(" FAILED: " + e.message);})

PouchDb with SQLite, out of memory error on query

I have a pouch database that contains hundreds documents of 1 Mo, so the total size is ~100 Mo. It's an Android Cordova app and I'm using:
PouchDb 5.4.5
cordova-plugin-sqlite-2 1.0.4
Each document is like this:
{
data: '...', // Very long string of 1 Mo
filename: 'myFile', // Name of a file
index: 34 // An integer
}
I'd like to do a query on this db to list all documents indexed by filename and index, without retrieving data property which is heavy.
So I did a view like this:
var designDoc = {
_id: '_design/getByFilenameAndIndex',
views: {}
};
mapFunction = function(doc){
emit(doc.filename + '/' + doc.index);
}
designDoc.views['getByFilenameAndIndex'] = { map: mapFunction.toString() };
myDb.put(designDoc);
But when I try to query my db, like this:
myDb.query('getByFilenameAndIndex', {include_docs: false}).then(function(docsByFilenameAndIndex){
console.log(docsByFilenameAndIndex);
});
I get the following error:
Why I'm having this error ? I still have 900 Mo of ram available on my device (Android 6), and I'm not even including docs. If my db is less than 20 Mo it works fine.
It was working fine too with db of 40 Mo before using SQLite plugin, but I can't work without it since I need more than 50 Mo database.
Edit: I tried to do this and I get the error too: myDb.allDocs({include_docs: false});
Instead of directly inserting large binary strings into your documents, you should use attachments. These will ensure that when you do operations that don't touch attachments (such as queries, allDocs(), etc.), PouchDB won't read the attachments into memory. This should fix your Out Of Memory issues.

Delete pre-populated sqlite database when update in cordova

I have completed my project which contains a pre populated sqlite database.
I have found this plugin and it works for me nicely. Lifehelper's products are truly remarkable and they have maintain pretty nice documentation.
However, when i open database, i use this format which is suggested their documentation for pre populated database.
var db = window.sqlitePlugin.openDatabase({name: "my.db", location: 'default', createFromLocation: 1});
However, my database is only for information. Application only read data from pre populated database. There is no any insert, update or delete query to keep per any user information.
In every new version pre populated data could modified, updated by me. So I need completely new app install along with new database when user will update their app.
But unfortunately, my app could not delete previous database. Changing version code also not working.
Someone suggest me, its about location which i am using to open database. But i can not understand how should dealing with it.
Help me, best answer is more important to me. Thanks.
To avoid increasing app size, you can do something like this:
function cleanupDatabases() {
var oldDatabases = ["2.1.0.db", "2.0.0.db"];
if (window.sqlitePlugin !== undefined) {
angular.forEach(oldDatabases, function(db) {
window.sqlitePlugin.deleteDatabase({
name: db,
location: 2
}, function() {
console.log("%c " + db + " is deleted", "background: green; color: white");
}, function(error) {
console.log("%c " + db + " could not be deleted", "background: red; color: white", error);
});
});
}
};

Sencha touch app in offline

My app is almost completed with sencha touch2.3, now I want to make it working in offline mode.
I need to load lot of data and images from my server application, It's working fine in online mode.
Problems I need to solve and what I have done
1. Need to store data in websql (using sql proxy) when there is a network.
I did this by.. if there is a network,I am loading online store and adding all the record to offline store.
Ext.getStore('foodGroup').load({
callback: function(records, operation, success) {
var offFoodGrup = Ext.getStore('offFoodGroup');
offFoodGrup.add(records);
offFoodGrup.sync();
offFoodGrup.load();
}
},
scope: this
});
2. I need to update the offline data if needed, I tried but it's not working. It adds duplicate data.
Ext.getStore('foodGroup').load({
callback: function(records, operation, success) {
var offFoodGrup = Ext.getStore('offFoodGroup');
if(records.length != (localStorage.offFoodGroup || 0)){
offFoodGrup.removeAll();
offFoodGrup.sync();
offFoodGrup.load({
callback: function(offRecords, operation, success) {
offFoodGrup.add(records);
offFoodGrup.sync();
offFoodGrup.load();
localStorage.offFoodGroup = offFoodGrup.getAllCount();
},
scope: this
});
}
},
scope: this
});
3. I need to show lot of images offline, so I though converting image url to base64 string may solve my problem. How can I do this in following code.
Ext.define('MyAPP.view.PhotoContainer', {
extend: 'Ext.Container',
xtype : 'photoContainer',
config:{
tpl: Ext.create('Ext.XTemplate',
'<ul class="foodList">',
'<tpl for=".">',
'<li class="foodContainer" code="{code}">',
'<img class="food" src="'+localStorage.httpServerPrefix+'food/showImage/{code}" alt="{name}"/>',
'<p code="{code}" class="foodnamestyle">{[this.getpreferedlanguage(values)]}</p>',
'</li>',
'</tpl>',
'</ul>'
}),
store : 'FoodStore'
}
});
It would be helpful to see your code for the FoodGroup model and online and offline stores if thats ok.
With the image in base64 format just use a data url like this:
<img src="data:image/png;base64,{Your Base64 Image Data}"/>
Hope that helps, and if you can post your models and stores I might be able to help with that.
Thanks,
Tristan

Categories

Resources