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

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 ?

Related

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.

Where to find SQLite database in Ionic2/3 project?

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.

Ionic reading data from SQLite during login in Android

Im facing a problem reading the already set data from previous login after user abruptly switches from my App into another or restarts the phone. The data I've set after successful login does get saved in the SQLite database.
.controller('LoginCtrl', function($scope, $ionicPopup, $state,$http,ServerEndPoint,localStorageService,$cordovaGeolocation,$ionicActionSheet,dataShare,$ionicPush,loading,$rootScope,$cordovaSQLite) {
$scope.data = {};
//Does not work
$scope.init = function()
{
$scope.load();
};
if(localStorageService.get("tradie_id") !== null && localStorageService.get("phone_no") !== null) {
$state.go('menu.map');
}
//This is called from login form submit button click
$scope.authenticateUser = function(loginForm){
//Authenticating user from the server, after successful login
//This one works
$scope.addInfo(res.data.user_id,res.data.first_name,res.data.phone_no,status);
$state.go('menu.map');
}
$scope.addInfo = function(user_id,first_name,phone_no,status){
var query = "INSERT INTO user_data(user_id,first_name,phone_no,status) VALUES(?,?,?,?)";
$cordovaSQLite.execute(db,query,[user_id,first_name,phone_no,status]);
$scope.load();
}
$scope.load = function(){
$scope.alldata = [];
$cordovaSQLite.execute(db,"SELECT * FROM user_data").then(function(result){
if(result.rows.length)
{
for(var i=0;i<result.rows.length;i++)
{
$scope.alldata.push(result.rows.item(i));
}
localStorageService.set("user_id", $scope.alldata[0].tradie_id);
localStorageService.set("first_name", $scope.alldata[0].first_name);
localStorageService.set("phone_no", $scope.alldata[0].phone_no);
}else
{
console.log("No data found");
}
},function(error){
console.log("error "+err);
})
}
})
Any suggestions or pointers to a sample source code is highly appreciated. I'm using ionic version 1.
I think you didn't create or open the db when app ready first:
var db = $cordovaSQLite.openDB({ name: "my.db" });

pouchdb not working on ios safari

I am trying to write a cordova application that uses pouchdb as offline DB (without sqlite). my app works from all browsers and from android but not ios. am not sure why is that and what to do to fi it! here is my code to initialise the db:
app.onDeviceReady = function() {
try {
console.log("starting db");
window.offline_db = new PouchDB('offline_db');
if (window.offline_db.adapter) {
console.log("db started!");
}
offline_db.get("op_settings", function(err, doc) {
if (err) {
return console.log(err);
}
console.log(doc);
});
} catch (e) {
console.log(e);
}};
this does not return the doc, even though when I save the doc it says it was saved successfully, it only gives a promise but no result.
Also on safari Mac I can see the db content in debugger, but on safari ios I dont have this tab under resources
It seems, that you have put your callback function on the place of options to be passed(optional):
PouchDB get
Please try
offline_db.get("op_settings", null, function(err, doc)
it should work.

How to Read & Write in Internal DataBase with PhoneGap?

I have an internal SQLite DB under "Assets" Folder in which i have stored 100 usernames & corresponding Passwords, How do i access it via phoneGap.? I have read
http://docs.phonegap.com/en/1.6.1/cordova_storage_storage.md.html#Storage
connecting to sqlite database from phonegap html file for android
http://wiki.phonegap.com/w/page/16494756/Adding-SQL-Database-support-to-your-iPhone-App
Am still not able to figure out How to connect to my internal DB which is already Created, dbname = userauthTablename = regparameters = usrnm , psw Any Answer is Highly EncouragedAm using this here.
/**
* Creates / Opens a connection to DB
*/
DB.openDB = function() {
try {
if (!window.openDatabase) {
//alert('Cannot open database!');
} else {
var shortName = 'db_name';
var version = '1.0';
var displayName = 'DBNAME';
var maxSize = (DEVICE_TYPE == DEVICE_ANDROID || DEVICE_TYPE == DEVICE_ANDROID_TAB) ? 5242880 : 1000000; ////819200; //65536; // in bytes // increased to support Android//163840; //
this.vocabDB = window.openDatabase(shortName, version, displayName, maxSize, this.DBCreated);
this.DBSupported = true;
}
} catch(e) {
//console.log("DB Error handling code goes here.");
//console.log(e);
return;
}
}
Well, you can do a window.openDatabase() to a database in the assets folder. You'll need to copy it to the right place so that the WebView will load it. Check out this:
http://gauravstomar.blogspot.ca/2011/08/prepopulate-sqlite-in-phonegap.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+GauravSTomarBootstrappingIntelligence+(Gaurav+S+Tomar+:+Bootstrapping+Intelligence)
post as Gaurav gives you code to do this on both Android and iOS.

Categories

Resources