I am trying to use SQLLItePlugin for Android but its not working. I will list my steps:
1. I have installed cordova pjhonegap from phonegap. I am developing my mobile app Phonegap, html5, javascript, css3 using Netbeans as IDE.
2. Downloaded plugin from https://github.com/brodysoft/Cordova-SQLitePlugin.
3. Added SQLitePlugin.js to js folder of project.
4. Added com.brodysoft.sqlitePlugin.file=https://github.com/brodysoft/Cordova-SQLitePlugin.git in plugin.properties.
5. Am opening database on deviceready as
var app = {
initialize: function () {
this.bindEvents();
},
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function () {
app.receivedEvent('deviceready');
var db = window.sqlitePlugin.openDatabase('gdata.db');
console.log('ready');
db.transaction(function (tx) {
tx.executeSql('DROP TABLE IF EXISTS test_table');
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
// demonstrate PRAGMA:
db.executeSql("pragma table_info (test_table);", [], function (res) {
console.log("PRAGMA res: " + JSON.stringify(res));
});
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function (tx, res) {
console.log("insertId: " + res.insertId + " -- probably 1");
console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
db.transaction(function (tx) {
tx.executeSql("select count(id) as cnt from test_table;", [], function (tx, res) {
console.log("res.rows.length: " + res.rows.length + " -- should be 1");
console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
});
});
}, function (e) {
console.log("ERROR: " + e.message);
});
});
},
// Update DOM on a Received Event
receivedEvent: function (id) {
var parentElement = document.getElementById(id);
console.log('Received Event: ' + id);
}
};
app.initialize();
Running the build on android devvice directly.
It keeps on throwing the error
Uncaught TypeError: Object # has no method 'exec'
(13:52:13:450 | error, javascript)
at SQLitePlugin.open (www/js/libs/SQLitePlugin.js:112:15)
at SQLitePlugin (www/js/libs/SQLitePlugin.js:54:10)
at (anonymous function) (www/js/libs/SQLitePlugin.js:425:14)
at (anonymous function) (www/js/libs/SQLitePlugin.js:30:20)
at createandpopulatedb (www/js/dborarray.js:30:30)
at onDeviceReady3 (www/dborarray.html:96:33)
at onload (www/dborarray.html:16:155) SQLitePlugin openargs: {"name":"gdataenter code here.db"} (13:52:19:609) at
www/js/libs/SQLitePlugin.js:39
Can somebody help.
try thhis
window.sqlitePlugin.openDatabase({name: "gdata.db"});
instead of this
window.sqlitePlugin.openDatabase('gdata.db');
Related
i'm learning with electronjs and sqlite3 and i used vue too, when i tried to to do tb.push in db.each the message was "undefined this.tb", i could fix it with no understanding how
so why the code below works
var app=new Vue({
el:'#resultDiv',
data:{
message:'hi',
tb:[],
},
mounted(){
var tb=[];
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');
db.serialize(function() {
db.run("CREATE TABLE lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
var textee;
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
textee=row.id + ": " + row.info;
console.log(textee);
tb.push(textee);
});
});
db.close();
this.tb=tb;
}
});
but this below does not
var app=new Vue({
el:'#resultDiv',
data:{
message:'hi',
tb:[],
},
mounted(){
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');
db.serialize(function() {
db.run("CREATE TABLE lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
var textee;
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
textee=row.id + ": " + row.info;
console.log(textee);
this.tb.push(textee);//this not working
});
});
db.close();
}
});
This is because in your non-working example
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
textee=row.id + ": " + row.info;
console.log(textee);
this.tb.push(textee);//this not working
});
the keyword this doesn't refer to the vue component, but to the execution context of the each-function.
This is caused by using the function() {}-way of describing anonymous functions.
If you'd instead use the arrow-function way to express the callback:
db.serialize(() => {
db.each("SELECT rowid AS id, info FROM lorem", (err, row) => {
textee=row.id + ": " + row.info;
console.log(textee);
this.tb.push(textee);//this now working
});
});
this now refers to your vue component's execution context and your code should work. Note that I had to use arrow function expression's for the serialize-function as well, otherwise this would have referred to that functions execution context instead.
Alternatives
If you, for some reason, cannot use the arrow function expression, you have several alternatives. You could explicitly bind this to the vue context like so:
db.serialize((function () {
db.each(..., (function (err, row) {
...
}).bind(this));
}).bind(this));
I don't particularly like this approach.
Or you could capture the this context in a variable under the closure of the mounted-method.
mounted() {
var self = this;
...
db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
textee=row.id + ": " + row.info;
console.log(textee);
self.tb.push(textee);
});
}
I have added grunt jshint task to my grunt. I created custom reporter to send out jsHint output as email. My custom reporter function is invoked. But no emails are coming through. There are no errors in the code.
Grunt version: "grunt": "^0.4.5",
"nodemailer": "^1.11.0",
"nodemailer-sendmail-transport": "^1.0.0"
Here is the sample code:
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var async = require('async');
module.exports = {
reporter: function (errors) {
var len = errors.length,
str = '';
var items = [1];
errors.forEach(function (r) {
var file = r.file,
err = r.error;
str += file + ": line " + err.line + ", col " +
err.character + ", " + err.reason + "\n";
});
if (str) {
str += "\n" + len + " error" + ((len === 1) ? "" : "s") + "\n";
}
var transporter = nodemailer.createTransport( smtpTransport( {
service: "gmail",
secureConnection: false, // use SSL
port: 587, // port for secure SMTP
auth: {
user: "<my gmail username>",
pass: "<gmail account password>"
},
tls:{
ciphers:'SSLv3'
},
logger: true, // log to console
debug: true // include SMTP traffic in the logs
}));
// setup e-mail data with unicode symbols
var mailOptions = {
from: '<sender address>',
to: '<recipient address>',
subject: 'Hello', // Subject line
text: "why are you not working"
/* text: str */// plaintext body
/*html: '<b>Hello world</b>' // html body*/
};
async.eachSeries(items, function (item, next) {
transporter.sendMail(mailOptions, function(error, response){
// THIS CALLBACK IS NOT CALLED AT ALL
if(error){
console.log(error);
}else{
console.log("Message sent");
}
next(null);
});
}, function(err){
// All tasks are done now
console.log('All tasks are done now');
});
}
};
with async or without async doesn't matter. No emails are coming. I tried bye turning on the "Receive emails from unsecured apps" by following another stackoverflow post. That also did not help.
I would like to know is this correct approach or not? Any help/input is appreicated.
I am trying to develop an android mobile application using the PhoneGap framework, and I want to synchronize my table of local database(of my phone) to the server database.
this is my code, but this code allow me to send only one line of table how can i send all line of table.
$.ajax({
type: 'POST',
data: col1+'&lid='+col2,
url: 'http://your-domain.com/comments/save.php',
success: function(data){
console.log(data);
alert('Your data was successfully added');
},
error: function(){
console.log(data);
alert('There was an error adding your data');
}
});
Try to select the data from the localDatabase and send it row by row, here's an example:
db.transaction(function(tx) {
Squery = 'SELECT * FROM news WHERE category_id ='+lid;
tx.executeSql(Squery,
null,
function(tx, results)
{
for(i=0; i<results.rows. length; i++){
row = results.rows.item(0);
$.ajax({ -- your code using row['name_of_column'] -- })
}
},
console.log('error')
});});
sorry for my miss understoods...
If you are using phonegap i guess you are using the Storage feature: http://docs.phonegap.com/en/2.2.0/cordova_storage_storage.md.html#Storage
folowing one of theirs examples, you can do something like:
function queryDB(tx) {
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
//do you ajax request....
...
data: {
rows : results.rows
}
...
}
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(queryDB, errorCB);
other option is to send a simple array...
function querySuccess(tx, results) {
var myRowsIds = [];
var len = results.rows.length;
for (var i=0; i<len; i++){
myRowsIds .push( results.rows.item(i).id )
}
//do you ajax request....
...
data: {
rows : myRowsIds
}
...
}
hope it helps!
I copy code from: How does the messages-count example in Meteor docs work? it does not work. client call Counts.find().count() method, I expect it to output 1 but the result is 0 ,can you tell me why?
//server code
if (Meteor.is_server)
{
Meteor.startup(function (){
console.log("server is startup...");
Messages = new Meteor.Collection("messages");
if(Messages.find().count() == 0){
for(var i=0;i<7;i++){
Messages.insert({room_id:"00"+i,text:"message "+i});
}
}
console.log("room_id:001 messages count="+Messages.find({room_id:"001"}).count());
//print--->room_id:001 messages count=1 (it's ok)
Meteor.publish("counts-by-room", function (roomId) {
var self = this;
var uuid = Meteor.uuid();
var count = 0;
var handle = Messages.find({room_id: roomId}).observe({
added: function (doc, idx) {
count++;
self.set("counts", uuid, {roomId: roomId, count: count});
self.flush();
},
removed: function (doc, idx) {
count--;
self.set("counts", uuid, {roomId: roomId, count: count});
self.flush();
}
// don't care about moved or changed
});
// remove data and turn off observe when client unsubs
self.onStop(function () {
handle.stop();
self.unset("counts", uuid, ["roomId", "count"]);
self.flush();
});
});
});
}
//client code
if (Meteor.is_client)
{
Meteor.startup(function () {
Counts = new Meteor.Collection("counts");
Session.set("roomId","001");
Meteor.autosubscribe(function () {
Meteor.subscribe("counts-by-room", Session.get("roomId"));
});
console.log("I client,Current room "+Session.get("roomId")+" has "
+ Counts.find().count() + " messages.");
//print--->I client,Current room 001 has 0 messages.(trouble:I expect it to output "...has 1 messages" here)
});
}
I try many times and I find the bug.
change the client code to like below,it will print the correct result.
//client code
Meteor.startup(function () {
Counts = new Meteor.Collection("counts");
Session.set("roomId","001");
Meteor.autosubscribe(function () {
Meteor.subscribe("counts-by-room", Session.get("roomId"));
data = Counts.findOne();
if(data){
console.log("I client,Current room "+Session.get("roomId")+" has "
+ data.count + " messages.");
//print--->I client,Current room 001 has 1 messages.(now it's ok!)
}
})
});
Try it like that
Counts = new Meteor.Collection("counts-by-room"); /* correct name of the collection */
/*... subscribing stuff */
Counts.findOne('counts') /* this is the count you published */
This is my javascript for Adobe Air:
$(document).ready(function(e) {
setupDB();
var tasks = getTasks();
$("#tasks").empty();
var numRecords = tasks.data.length;
for(i=0; i<numRecords; i++) {
$("<li/>").append('<span>'+ tasks.data[i].id +' - '+ tasks.data[i].task +'</span>').appendTo("#tasks");
}
});
var db = new air.SQLConnection();
function setupDB() {
var dbFile = air.File.applicationStorageDirectory.resolvePath("airTasks.db");
try {
db.open(dbFile);
} catch (error) {
alert("DB Error: "+ error.message);
alert("Details: "+ error.details);
air.trace("DB Error: "+ error.message);
air.trace("Details: "+ error.details);
}
}
function getTasks() {
var query = new air.SQLStatement();
query.sqlConnection = db;
query.text = "SELECT id, task FROM tasks";
try {
query.execute();
} catch(error) {
alert("Error getting tasks from DB: "+ error.message +", DETAILS: "+ error.details);
air.trace("Error getting tasks from DB: "+ error);
air.trace(error.message);
return;
}
return query.getResults();
}
Here is a screenshot of the Database Version 3 - airTasks.db
I keep getting:
ERROR: Table "tasks" not found.
Double check that your filepath being opened actually exists. If it doesn't, SQLite creates one for you.
Try this to troubleshoot before you call db.open():
alert(dbFile);
Is this the correct path to your db?