So i'm using multiple if statements to draw data from a database based on the users search criteria.
What i'm struggling with is
if(request.getParameter("searchProperty")!= ""){
SearchStatement = "town_city = '" + request.getParameter("searchProperty") + "'";
if(request.getParameter("bedrooms") != "0"){
SearchStatement += " AND bedrooms = '" + request.getParameter("bedrooms") + "'";
}
}
with the idea that this concatenates a string to use as a query in the database, and bring back the results the user is searching for (this is a property searching website). I thought i'd done the if statement correctly. From what i understand, from what i've put, if the user were to select 0 in bedrooms it should return ALL results, but instead it returns NONE (who wants a house without a bedroom..) Can somebody explain what's going wrong please?
here's where the SQL statement is built and input
MyProperties = bookSQL.executeQuery("SELECT * FROM PROPERTIES WHERE " + SearchStatement);
with the expected outcome being, for example
SELECT * FROM PROPERTIES WHERE Location = 'input' AND Bedrooms = 'value'
unless value = 0 where it should just be
SELECT * FROM PROPERTIES WHERE Location = 'input'
i think the problem is with this statement,
request.getParameter("bedrooms") != "0"
should be something like this ,
(!request.getParameter("bedrooms").isEmpty())
Remember you are comparing the strings
so if is "0"
if(request.getParameter("bedrooms").equals("0")){
return SearchStatement ;
}
else {
SearchStatement += " AND bedrooms = '" + request.getParameter("bedrooms") + "'"
}
Hope this helps!!
Related
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.
I am writing a script to take a stock number, loop through existing stock numbers until a match is NOT found, then assign that unique stock number to the record. My problem is that the usual data[i][2] doesn't seem to reference a 'query' the same way that Apps Script would reference an array.
Fair warning, I'm trying to expand my Apps Script skills in to broader Javascript so I there's a good chance I'm doing it all wrong - I'm all ears if you tell me I'm doing this all incorrectly!
Using the log: data[i][2] gives me 'undefined' whereas data[2] gives me all fields of the third item in my query. Based on this I feel like I just need to learn how to reference it properly.
//Querying my datasource as 'var data'
var query = app.models.UsedVehicles.newQuery();
query.filters.ParentDealType._contains = prefix;
var data = query.run();
//Returns four records which is correct.
var testStockNo = prefix+month+countstring+year;
console.log("Test Stock Number " + j + ": " + testStockNo);
for (i = 0; i < data.length; i++){
console.log("data[i][2]: " + data[i][2]); //results: undefined
console.log("data[2]: " + data[2]); //results: all fields of 3rd query result.
if(data[i][2] === testStockNo){
k++;
break;
}else{
console.log("No Match");
}
}
Even if testStockNo equals the value in field:TStockNo, the log displays:
Test Stock Number 1: C1200118
data[i][2]: undefined
data[2]: Record : { TIndex: 8, TVin8: HS654987, TStockNo: null,
TParentStkNo: GSD6578, TYear: 2010, TMake: NISSAN, TModel: PICKUP,
TMileage: 24356, ParentDealType: C}
No Match
Issue/Solution:
query.run() returns array of records and NOT a array of arrays(2D). You should access the Record value using it's key instead of a index.
Snippets:
console.log("data[i][TStockNo]: " + data[i]['TStockNo']);
console.log("data[i].TStockNo: " + data[i].TStockNo);
console.log("data[2]: " + data[2]);
References:
Query#Run
I have a script that will insert the checked checboxes and radios in the value() of an input tag.
var burgernavn = meat + " with " + bread + " and " + onion + tomato + cheese + salad;
$('#burger-navn').val(burgernavn);
Onion, tomato, cheese and salad needs to have1 " ," between them, except the last two who need " and " between them.
Thats the first thing.
Second thing is that these variables represent checkboxes, so they can be undefined, in which case they should not be put into $('#burger-navn').val(). They can all be undefined, in which case no commas or "and" should be put in.
I hope this is accomplishable.
Capture all checked values in an array (this makes sure that whatever values in this array, all are defined). Also, it will give you count of values that you need to pass to input box.
var checkedValues = document.getElementsByClassName('checkbox');
Iterate over this array, check for last values. (Check my comments in below code snippet)
var commaValues = "", otherValues= "";
//we are iterating only until (total values -2)
for(var i = 0; i < checkedValues.length - 2 ; i++){
//append comma with each value
commaValues += checkedValues[i].concat(",");
}
//append 'And' for last two values if total valuesa re more than one
if(checkedValues.length > 1){
otherValues = checkedValues[checkedValues.length-1].concat("and", checkedValues[checkedValues.length])
}
else if(checkedValues.length == 1){
otherValues = checkedValues[0];
}
//finally concat both strings and put this concated string in input
$('#burger-navn').val(otherValues.concat(commaValues));
So, I hope you got the idea. This code snippet might need a bit tweak since I didn't had your html code and sample data, but it is easily doable using this snippet as reference. Cheers
You can do this pretty easy with jQuery $.map.
var checkboxes = $('input:checkbox');
var commaString = $.map($('input:checkbox'), function( ele, i ) {
return $(ele).val() + (i + 2 == checkboxes.length ? " and" : (i + 1 != checkboxes.length) ? ",": "");
}).join(" ");
I'm writing a quiz app using phonegap & jquery. I have a number of categories of question from which the user can select (via checkboxes), and want to build a database query to pull back the questions from the relevant categories based on their selection. The code below gives the gist of what I'm trying to achieve, but I cannot pass queryString into the executeSql() method. Is there a straightforward way of doing this that I'm missing? Thanks, Nick
function queryDB4(tx){
var queryString = "SELECT * FROM SBA_TABLE WHERE (";
if (toggle_cardiology == 1)
{
queryString += "cat_cardiology = 1 AND";
}
else if (toggle_respiratory == 1)
{
queryString += "cat_respiratory = 1"
}
// repeat for other categories
queryString += ") LIMIT 10;";
//alert(queryString);
tx.executeSql(queryString, [], querySuccess3, errorCB);
}
I think the problem is that your query string is invalid. Here's one example string that your code could produce:
SELECT * FROM SBA_TABLE WHERE (cat_cardiology = 1 AND) LIMIT 10;
There are a number of problems:
Append 1=1 (or any other statement that is always true) to handle the trailing AND.
Omit the else clause to allow combinations of checkboxes, not just the first one that is checked.
You need a space after AND to avoid producing ANDcat_respiratory.
Putting it together:
if (toggle_cardiology == 1)
{
queryString += "cat_cardiology = 1 AND ";
}
if (toggle_respiratory == 1)
{
queryString += "cat_respiratory = 1 AND ";
}
queryString += "1 = 1) LIMIT 10";
Good day,
I have two classes, one representing Hospital Admissions (called Ricoveri) and another one representing Discharges (called Dimissioni). Both have a property named Nosologico, representing an admission's unique identifier.
I have written a Javascript function, inspired by one of Luca Garulli's posts, to recursively create Edges between the two classes on the Nosologico property.
function code is as follows:
var g=orient.getGraph();
var b=g.command('sql','select from Ricoveri')
for(i=0; i<b.length; i++){
var id=b[i].getProperty("Nosologico").toString();
var rid=b[i].getId().toString();
var r=g.command("sql", "select from Dimissioni where Nosologico = '" + id + "'");
if(r.length>0){
var sql2="create edge dimesso from " + rid + " to (select from Dimissioni where Nosologico = '" + id + "')";
g.command('sql',sql2);
}
}
The function gets created successfully and executes without errors but it doesn't create any Edge.
Occasionally the whole server gets frozen and I have no other option than to kill it and restart it.
Is there anything wrong with the function?
Thanks.
QQ
first of all I suggest, if you heaven't done already, to create UNIQUE_HASH_INDEXes on nosologico property in both classes.
Then assuming that every nosologico in Dimissioni is also present in Ricoveri, and assuming that there are fewer Dimissioni than Ricoveri, the following query should be more efficient.
var g=orient.getGraph();
var b=g.command('sql','select from Dimissioni');
var id="";
var rid="";
var sql2="";
for(i=0; i<b.length; i++){
id=b[i].getProperty("Nosologico").toString();
rid=b[i].getId().toString();
sql2="create edge dimesso from (select from Ricoveri where Nosologico = '" + id + "') to " + rid;
g.command('sql',sql2);
g.commit();
}
let me know if this helps.
Ivan
maybe i'm wrong but, you do the query
var r=g.command("sql", "select from Ricoveri where Nosologico = '" + id + "'");
then you verify the length of the results, but it's pretty obvious that is > 0 (var id is coming from var b that is coming from the first query on Ricoveri).
So I'm saying, maybe in the var r you wanted to query from Dimissioni?
var r=g.command("sql", "select from Dimissioni where Nosologico = '" + id + "'");