I am new to Node server. In my program I fetched some data from data base as
connection.query("SELECT * from login where username='" + uname + "' and password='" + pwd + "' ", [uname, pwd], function (err, rows)
{
var employees = (JSON.stringify(rows));
console.log("Inside server "+employees )
});
Now in console i got some thing like
[{"user_id":7,"username":"vb","password":"vbv"}]
Now my Question is how i could get the value username from employees. I have tried few things like
employees[0]["username"] OR employees.username etc..
Because you stringify it, it becomes string so you can't access like that. Just use it as is.
JSON.stringify(rows) only allow you to pretty print the data.
To access it you were right with employees[0]["username"]
connection.query("SELECT * from login where username='" + uname + "' and password='" + pwd + "' ", [uname, pwd], function (err, employees)
{
console.log("Inside server " + JSON.stringify(employees));
console.log('Employee 1: ' + employees[0]["username"]);
});
Related
flow.set("payload","msg.payload.payload");
flow.set("humidity","msg.payload.humidity");
var date = new Date().getTime();
msg.topic= "insert into sensor(temp, humidity, date) values('"+msg.payload.payload+"', '"+msg.payload.humidity+"', '"+date+"')";
return msg;
this is my function code
i receive data to {"_msgid":"81665f152edd9336","payload":"25.20","topic":"rpi-dht22","humidity":"39.30","isValid":true,"errors":2,"sensorid":"dht22"}
but saw in database undefined, undefined, 1636534958644
what is problem?
You have 2 problems with the function node.
Firstly you are inserting strings into the flow context rather than the values from the incoming message, you should not be wrapping the value argument in quotes.
Secondly you have an extra payload in the msg object keys.
It should probably look like this:
flow.set("payload",msg.payload);
flow.set("humidity",msg.humidity);
var date = new Date().getTime();
msg.topic= "insert into sensor(temp, humidity, date) values('"
+ msg.payload + "', '"
+ msg.humidity + "', '"
+ date + "')";
return msg;
what dos this error mean? and how can I fix it?
I am trying to save data into my database, but I want to check if there already data exist there. and if there is, I want to update it.
Thank you for your help! It will be highly appreciated!
LogCat:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
at com.musicapp.android.musicapp.SQLDataBase.App_Theme_Color(SQLDataBase.java:220)
at com.musicapp.android.musicapp.Settings.ThemeCustomAdapter$1$1.run(ThemeCustomAdapter.java:50)
CODE:
public void App_Theme_Color(int color1, int color2, int color3) {
220 SQLiteDatabase db = this.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_01,color1);
contentValues.put(COL_0002,color2);
contentValues.put(COL_0003,color3);
String query = (" SELECT " + COL_01 + "," + COL_0002 + "," + COL_0003 + " FROM " + Table_Name3);
Cursor cursor = db.rawQuery(query,null);
if (cursor.getCount() > 0){
String updateQuery = (" UPDATE " + Table_Name3 + " SET " + COL_01 + " ='" + color1 + " '" + "," + COL_0002 + " ='" + color2 + " '" + "," + COL_0003 + " ='" + color3 + " '");
db.update(Table_Name3,contentValues," WHERE",null);
db.execSQL(query);
db.execSQL(updateQuery);
}else{
db.insert(Table_Name3,null,contentValues);
}
cursor.close();
db.close();
}
In your code at line 220, you cannot call getReadableDatabase on this, as getReadableDatabase is a method that belongs to the SQLiteOpenHelper class. You must instantiate an object of SQLiteOpenHelper, or some sub-class of it, and then call this method on that object.
I have a Node.js app that is writing data to a MySQL backend. One field is an array I stringify. I can see in the workbench the data is correct when in the database. However when I retrieve it I am getting an error when I try to parse it.
"Unexpected token o in JSON at position 1"
If I log the result it shows up as [Object Object].
From what I read online this means it is already a JS object and I do not need to parse it. However I cannot find anything about how to get access to the data.
process: function (bot, msg, suffix) {
var ftcmds = suffix.split(" ", 1);
var ftName = ftcmds[0];
var ftArray;
var selectSQL = "SELECT FireTeam FROM fireteam WHERE Name = '" + ftName + "'";
var updateSQL = "UPDATE fireteam SET FireTeam = '" + ftArray + "'WHERE Name = '" + ftName + "'";
mysqlcon.query(selectSQL, function (err, result) {
console.log("Result |" + result);
console.log("Error |" + err);
if (err) {
console.log("Caught Error " + err + " " + msg.author);
}
else {
console.log("Recovered result " + result);
ftArray = result;
console.log("Attempting to update array");
ftArray.push(msg.author.id);
console.log("updated array " + ftArray);
var jsonArray = JSON.stringify(ftArray);
mysqlcon.query(updateSQL, function (err, result) {
console.log("Result |" + result);
console.log("Error |" + err);
if (err.toString().indexOf(dupErr) != -1) {
msg.author.send("Could not find that fireteam");
console.log("Error: Did not locate the requested name " + msg.author)
} else if (err) {
console.log("Caught Error " + err + " " + msg.author);
}
else {
msg.author.send("You have joined Fireteam " + name + ". I will setup a group chat on " + date + " if your team fills up.");
}
})
}
});
}
You should just be able to access it as an object, so if result has fields name and title you can just access them as:
var name = result.name
var title = result.title
I want to know how can I save result of MySql query in variable using node js
I used this to connect to mysql and get query
var mysql = require('mysql');
var express = require('express');
var app = express();
app.get('/informations', function (req, res) {
var connection = mysql.createConnection(
{
host : 'localhost',
user : 'root',
password : '',
database : 'dbUsers',
}
);
connection.connect();
var queryString = 'SELECT * FROM hpform';
connection.query(queryString, function(err, rows, fields) {
res.json(rows);
for (var i in rows) {
console.log('NomBase: ', rows[i].NomBase);
console.log('CheminHP: ', rows[i].CheminHP);
console.log('Chemin: ', rows[i].Chemin);
console.log('HPuser: ', rows[i].HPuser);
console.log('pass: ', rows[i].pass);
console.log('path_pub: ', rows[i].path_pub);
}
});
connection.end();
});
module.exports = app;
and I have this in result:
[{"HPId":16,"NomBase":"Base","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"P:\\\\EXSTREAM\\\\BASES\\\\MACSF_DB_EditiqueV9.5.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":21,"NomBase":"LMG","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"D:\\\\hp_LMG\\\\BaseAccess20160330.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":22,"NomBase":"Meriem","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"D:\\base_meriem\\Test.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":24,"NomBase":"bouygues","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"D:\\\\hp_LMG\\\\bouygues\\\\20160425 - PRODUCTION MEP FE_22606 - Ano314002 Libellé remboursement EDP Appro","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"},{"HPId":26,"NomBase":"hamza","CheminHP":"C:\\Program Files (x86)\\Hewlett-Packard\\HP Exstream\\HP Exstream 9.5.102","Chemin":"P:\\\\EXSTREAM\\\\BASES\\\\MACSF_DB_EditiqueV9.5.accdb","HPuser":"admin","pass":"admin","path_pub":"D:\\Users\\hbenkhal\\Desktop\\essaipub"}]
now I want to save my results as a request to use them in exec command something like this
child = exec("\"" + req.session.CheminHP + "/Packager.exe\" -APPLICATION=" + req.params.app + " -ACCESSDB=" + req.session.Chemin + " -EXSTREAMUSER=" + req.session.HPuser + " -EXSTREAMPASSWORD=" + req.session.pass + " -PACKAGEFILE=" + req.session.path_pub + "\\" + req.params.app + ".pub", function (error, stdout, stderr)
Can I have a suggestion to do this.
Thank you.
If you get only one row from your query try this:
connection.query(queryString, function(err, rows, fields) {
res.json(rows);
var elem = rows[0];
for (var prop in elem) {
req.session[prop] = elem[prop];
}
});
connection.end();
Thank you very much Nikita Namestnikov.
I used what you tell me to do and I add
console.log('NomBase: ' + req.session.NomBase);
console.log('CheminHP: ' + req.session.CheminHPBase);
console.log('Chemin: ' + req.session.CheminBase);
console.log('HPuser: ' + req.session.HPuserBase);
console.log('pass: ' + req.session.passBase);
console.log('path_pub: ' + req.session.path_pubBase);
to see if that work but there is a problem
When I Use req.session.NomBase, req.session.CheminHPBase,... in my routes it undefined
So all i want to do is to make a query to get all id's from the database and then i want to do my method for the id and update the row in the database.
So i created this code. But all it is doing is to get the ids and do not the nested query with the insertString1.
var mysql = require('mysql');
var _ = require("underscore");
var connection = mysql.createConnection({
host : 'myHost.net',
user : 'myUser',
password : 'myPassword',
database : 'myDatabase'
});
connection.query('SELECT * FROM `idmarkethash`', function (error, results) {
_.each(results, function (row) {
// here i want to calculate the new values of the row
// This insert query is just an example
var insertString1 = "UPDATE idmarkethash SET " +
"Price=" + mysql.escape(2.00) + ", " +
"Gradient=" + mysql.escape(2.00) + ", " +
"LastUpdated=" + mysql.escape("2015-08-31 00:00:11") + " " +
"WHERE Id=" + mysql.escape(181) + ";";
connection.query(insertString1, function (error1, result1) {
console.log(result1);
});
});
});
I tried the same with the async package:
var async = require('async');
// Packages are all included.
connection.query('SELECT * FROM `idmarkethash`', function (error, results) {
async.each(results, function (row, callback) {
// here i want to calculate the new values of the row
// This insert query is just an example
var insertString1 = "UPDATE idmarkethash SET " +
"Price=" + mysql.escape(2.00) + ", " +
"Gradient=" + mysql.escape(2.00) + ", " +
"LastUpdated=" + mysql.escape("2015-08-31 00:00:11") + " " +
"WHERE Id=" + mysql.escape(181) + ";";
connection.query(insertString1, function (error1, result1) {
console.log(result1);
callback();
});
});
});
With debugging i could see that the declaration of insertString1 is called, but the second query is not fired / or the loop continues without recognizing the second query.