Cordova sqlite plugin not working in device

i installed cordova sqlite plugin followed below tutorial

it is working when im doing

ionic serve

but it is not working after i install apk in mobile. please tell me the solution.

Cordova plugins wonā€™t work on browser (ionic serve) so it may not be working at all for you.

Can you verify the plugin libraries are included in your android build? How are you testing whether or not itā€™s working?

Hi,

i created apk. and install in my mobile. data in not able to insert in table. so i guess database is not creatingā€¦
please suggest how to test this and debugā€¦ this database.

libraries are hereā€¦ i checked

platforms\android\ant-build\classes\net\orworks\cordovaplugins\cordovasqlite

any errors while debugging?

no errors showing,

here is my code.
app.js

var db = null;
angular.module(ā€˜starterā€™, [ā€˜ionicā€™, ā€˜starter.controllersā€™,ā€˜ngCordovaā€™])

.run(function($ionicPlatform,$cordovaSQLite) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}

if (window.cordova) {
db = window.sqlitePlugin.openDatabase({ name: ā€œiautocount.dbā€, location: 1 }); //device
}else{
db = window.openDatabase(ā€œiautocount.dbā€, ā€˜1ā€™, ā€˜autoā€™, 1024 * 1024 * 100); // browser

}

});
})

and here is my controller.js

.controller(ā€˜SettingsCtrlā€™, function($scope, $http,$ionicLoading,$cordovaSQLite) {

  	$cordovaSQLite.execute(db, "drop table profile");
  				$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS profile (id integer primary key, profiledata text)");
  				proquery = "INSERT INTO profile (profiledata) VALUES (?)";
  				$cordovaSQLite.execute(db, proquery, ["testdatahere"]).then(function(res) {
  					//console.log("INSERT ID -> " + res.insertId);
  					//alert(res.insertId);
  						//window.alert(res.insertId)
  				  });

var que = ā€œSELECT profiledata FROM profileā€ ;
$cordovaSQLite.execute(db, que, ).then(function(res) {
if(res.rows.length > 0) {
$scope.profdata=res.rows.item(0).profiledata;
//window.alert($scope.profdata)
}
});

})

i m waiting can you please give me solutionā€¦

The SQL lite plugin will spit out logs showing that the DB was created. To see the logs you can do 1 of 2 thingsā€¦

  1. Install the app using ionic livereload (ionic run -l, or ionic emulate -lc if running on ios sim), to enable console logs from ionics livereload server. If this is unfamiliar to you then you may need to look at the ionic CLI README here .

  2. for ios, build the app on the ios simulator using Xcode, then you can trace the logs for the Sqllite log output.

Once you get some logs going paste them here.

@djett

i m trying in emulator , app is not loading in emulator. is it any other way to test. or what is wrong in my codeā€¦ can you tell me so i can fix it.

@jswebtech .If you using Sqlite Plugin.this is solutions you have to.Please follow the solution

@Thamil

i checked where to create db not understand.

below is my code can u tell me what is wrong what i will do to create db.

var db = null;
angular.module('starter', ['ionic', 'starter.controllers','ngCordova'])
.run(function($ionicPlatform,$cordovaSQLite) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
	if (window.cordova) 
	{
		db = window.sqlitePlugin.openDatabase({ name: "iautocount.db", location: 1 }); //device
		window.alert("Trying DB...")
    }
	else
	{
		db = window.openDatabase("iautocount.db", '1', 'auto', 1024 * 1024 * 100); // browser
	}
  });
})

Hi, just to point out that you can see the code provided in Quizionic App Template here: http://codecanyon.net/item/quizionic-a-quiz-app-template-for-ionic-framework-with-sqlite-database/14205904, that uses a pre-populated SQLite database file and also works on Ionic Serve under Chrome browser.

See the best starter with SQLite using ngCordova and services: https://github.com/jdnichollsc/Ionic-Starter-Template

Regards, Nicholls

Hi.

Did you find a solution? Iā€™m facing this exactly problem and I canā€™t understand what is happening.

Thanks

give me full code of Create and insert code for angular 2

Thanks,

1 Like

Hi,
iam stuck with issue from a week, please anyone help me outā€¦
Iā€™m working on an ionic appā€¦
Iā€™m currently having trouble with the iOS version.

When I open my dataBase with this command :

   var db= $cordovaSQLite.openDB({name:"olarcniapp.db", iosDatabaseLocation: 'Library'});

$cordovaSQLite.execute(db, query).then(function(result) {
window.alert(ā€œRecord Selected succefully :ā€+JSON.stringify(result));
console.log(ā€œRecord Selected succefully :ā€+JSON.stringify(result));
deferred.resolve(result);
}, function (err) {
window.alert(ā€œError is :ā€+JSON.stringify(err));
deferred.reject(null);
});
});

when i run it in xcode simulator ,This is telling me that the database was opened,
but insted of $cordovaSQLite.execute function . iam getting error message, code:ā€œ5ā€,message:"no such table:table name"