Javascript variable, different values, same line - javascript

First of all, let me apologize for the title, as it isn't so explanatory, but I could not say it in another way.
The deal is: I am doing a javascript application, in which I have an object called "ocorrencia", which was defined like this:
var ocorrencia = new Object();
that object has several children, being filled by a method:
ocorrencia.idOcorrencia = ""+ year + month + day + hour + minute + second + milisec;
idOcorrencia is the one I am having problems with, because I am running a DataBase insert with this value, and I use it 2 times in the same insert, like:
var sql = 'INSERT INTO OCORRENCIAS (id, ocorrencia, data, resolucao, urgencia, foto) VALUES (' + ocorrencia.idOcorrencia + ', "' + ocorrencia.descricao + '", "' + ocorrencia.data + '", "' + ocorrencia.resolucao + '", "' + ocorrencia.grauUrg + '", "' + ocorrencia.idOcorrencia + '.jpg"' +')';
The insert runs great, an I have all the data inserted in the DB, BUT "id" and "foto" (which were supposed to get equal values) are giving me different values by 2 or 3 miliseconds.
How can this happen, as I am not changing "ocorrencia.idOcorrencia" ?
This is beeing tested in an Android device.
EDIT: Tested on Windows browser and the problem doesn't appear to happen.
Thank you

I guess you fill idOcorrencia on runtime? So the lag is producing this difference.
Try using a hash for the id or set it before running the SQL-query.

Related

How to get data from a especif Time period in InfluxDB Node-RED

I'm trying to read data from InfluxDB v1.x but only in a especific time period, Example:
Get the values of temper01 from 2021-11-09/14:00:00 to 2021-11-10/14:00:00.
i know its possible with this code :
"WHERE time >= '2021-11-09T14:00:00Z' AND time <= '2021-11-10T14:00:00Z'" But i wanna know if it is possible to use a variable instead of the direct timestamp.
i've tried this code :
msg.query = "SELECT time , Temper01, Temper02, Temper03 FROM "
+ global.get("ID_device1")
+ " WHERE time >= "
+ msg.payload;
+ " AND time <= '2021-11-18T00:00:00.000Z' " ;
but dosent matter the msg.payload value, it just get data from the very first data stored, and when i try to single quote msg.payload i get the invalid operation: time and *influxql.VarRef are not compatible" error message.
Assuming that msg.paylaod looks something like 2020-11-18T00:00:00.000Z then you have not quoted the time string you are inserting into the query.
It should look like this:
msg.query = "SELECT time , Temper01, Temper02, Temper03 FROM "
+ global.get("ID_device1")
+ " WHERE time >= '"
+ msg.payload;
+ "' AND time <= '2021-11-18T00:00:00.000Z' " ;
The difference is the ' at the end of the string on the 3rd line and again at the start of the string on the last line.
Directly comparing a working version with the version that didn't work would have shown this.

Knex - Result of SUM queries between knex and mysql workbench return different values

I am currently facing this issue when summing fields. I am using Knex and Mysql.
When using Knex, I get this result for the LastYr column..
Knex Result:
But doing the same query in Mysql Workbench, I get this (which is what I want)
Mysql Workbench:
My query is this:
SELECT t1.*, IFNULL(t2.TotalUnits,0) AS TotalUnits, ROUND(IFNULL(t2.totalRevenue,0),0) AS TotalRevenue FROM (SELECT BranchDept, ForPeriod, SUM(SysGen) AS Gen, SUM(UserInput) as Live, SUM(PrevYear) AS LastYr, SUM(SysGen) - SUM(UserInput) AS Input FROM tbl_demand_forecast_details WHERE BranchDept='" + req.body.branchdept + "' AND ForPeriod >= '" + fystart + "' " +
"AND ForPeriod <= '" + fyend + "' GROUP BY BranchDept, ForPeriod " +
"ORDER BY ForPeriod) AS t1 " +
"LEFT OUTER JOIN " +
"(SELECT BranchDept, AsOfPeriod, SUM(UnitSales) AS totalUnits, SUM(PesoSales) AS totalRevenue FROM tbl_sales_history " +
"WHERE BranchDept='" + req.body.branchdept + "' AND AsOfPeriod >= '" + fystart + "' AND AsOfPeriod <= '" + fyend + "' GROUP BY AsOfPeriod) AS t2 " +
"ON t1.BranchDept = t2.BranchDept AND t1.ForPeriod = t2.AsOfPeriod
It seems knex is adding the previous value to the next in the LastYr column.
I tried this with knex.raw and using the knex methods, and I still get the wrong values.
This is driving me nuts for the past two days. Anyone point me to the right direction?
Okay. I finally got it.
The SUM query goes haywire when there are null values on the column being summed. Replacing those with zeroes fixed this issue.

binding variables to SQL statements using node-sqlite3

I am trying to convert this:
var query_string = 'SELECT protein_A, protein_B, PIPE_score, site1_A_start FROM ' + organism + PIPE_output_table +
' WHERE ' + score_type + ' > ' + cutoff['range'] + ' AND protein_A = "' + item + '" ' +
'UNION SELECT protein_A, protein_B, PIPE_score, site1_A_start FROM ' + organism + PIPE_output_table +
' WHERE ' + score_type + ' > ' + cutoff['range'] + ' AND protein_B = "' + item + '";';
db.each(query_string, function (err, row) {
...
To this:
var query_string = "SELECT protein_A, protein_B, PIPE_score, site1_A_start FROM $table WHERE $score_type > $score AND protein_A = '$protein'" +
" UNION SELECT protein_A, protein_B, PIPE_score, site1_A_start FROM $table WHERE $score_type > $score AND protein_A = '$protein'";
var placeholders = {
$table: organism + PIPE_output_table,
$score_type: score_type,
$score: cutoff['range'],
$protein: item
};
var stmt = db.prepare(query_string, placeholders, function(err) {
console.log(err);
stmt.each(function(err,row) {
...
})
}
but I keep getting this error:
Error: SQLITE_ERROR: near "$table": syntax error
But I am not sure what is syntactically wrong here since the format is as I have seen it in the API documentation. I have tried '?', '#', and ':' before each variables but none seem to be recognized.
What's wrong in my code?
Bind parameters only work for values in the WHERE clause. Table and column names (collectively called "identifiers") won't work.
"SELECT foo FROM bar WHERE this = $that" # OK
"SELECT foo FROM bar WHERE $this = 'that'" # Not OK
Normally you'd work around this by escaping and quoting identifiers and inserting them into the query. A good database library has a method call for this...
var this = db.quote_literal(input_column);
'SELECT foo FROM bar WHERE ' + this + ' = ?'
Unfortunately, node-sqlite3 doesn't appear to have one. :(
SQLite does provide a quoting function, the %w operator, but node-sqlite3 doesn't appear to make it available.
You'll have to write your own. Follow the instructions from this answer in Python and convert them to Javascript.
Ensure the string can be encoded as UTF-8.
Ensure the string does not include any NUL characters.
Replace all " with "".
Wrap the entire thing in double quotes.
I'm not very good with Javascript, so I'll leave you to code that.

error due to single quote in a parameter in url query string

I have a screen from which I am passing a parameter to other screeen. This parameter contains a single quote. (For eg. ABC's xyz). The other screen shows this paramter correctly on screen. However, I am not able to submit the request on load to servlet. I am not able to figure out why?
Tried using URL encoding, replacing ' with \' and replacing ' with %27. Didn't work for me. Plz help.
The code is as follows.
child page var scheme = document.forms[0].scheme.value;
window.location.href = redirectVal+".jsp?levelOfReport="
+ levelOfReport
+ "&circleCode="
+ circleCode
+ "&networkCode="
+ networkCode
+ "&moduleCode="
+ moduleCode
+ "&regionCode="
+ regionCode
+ "&branchCode="
+ branchCode
+ "&circleName="
+ circleName
+ "&moduleName="
+ moduleName
+ "&branchName="
+ branchName
+ "&Amount="
+ Amount
+ "&scheme="
+ scheme + "&fromDate=" + fromDate + "&toDate=" + toDate;
Child page
SubmitButton('./Report.do?method=getAgriProductWisePerformanceReport&levelOfReport=&scheme=<%=request.getParameter("scheme")%>
You're missing the end to your method call.
SubmitButton('./Report.do?method=getAgriProductWisePerformanceReport&levelOfReport=&scheme=<%=request.getParameter("scheme")%>
Map your quotes to eachother, you're telling it to do something like
SubmitButton('./SubmitButton('./Report.do?method=getAgriProductWisePerformanceReport&levelOfReport=&scheme=HTTP
You need to add an additional '); at the end of that line

Pass multiple parameters from ASP.NET to javascript function

Essentially what I'm trying to do is fairly simple....pass more than one parameter (4 total eventually) into a javascript function from my ASP.NET code behind.
What I've tried to do is this in the ASCX file...
function ToggleReportOptions(filenameText, buttonText) { /*stuff happens here*/ }
and this in the ASCX.cs file...
string testing123 = "testStringOne";
string testing124 = "testStringTwo";
optReportOptionsRunRealTime.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";
optReportOptionsOffline.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";
but this doesn't seem to work as in my output the first variable contains "testStringOne, testStringTwo" and the 2nd variable is "undefined"
Any help on clearing up my probably stupid issue here would be great (i'm very inexperienced with javascript, more of a .NET developer)
You've missed out a few single-quotes, meaning that you're passing a single string containing a comma rather than two separate strings. That is, you're passing 'testStringOne, testStringTwo' rather than 'testStringOne' and 'testStringTwo'.
Try this instead:
optReportOptionsRunRealTime.Attributes["onClick"] =
"ToggleReportOptions('" + testing123 + "', '" + testing124 + "')";
optReportOptionsOffline.Attributes["onClick"] =
"ToggleReportOptions('" + testing123 + "', '" + testing124 + "')";

Categories

Resources