1

It may be a beginner question, but I don't know how to use a variable inside a SQL query.

const CCqry = req.params.CC;  // It contains a string
var values =Object.values(DATA.laureates);
const result = alasql("SELECT * FROM ? WHERE bornCountryCode = \"%CCqry\" ", [values]);

The above one doesn't work for me, don't know what I am doing wrong.

However the below one works,

const CCqry = req.params.CC;  // It contains a string
var values =Object.values(DATA.laureates);
const result = alasql("SELECT * FROM ? WHERE bornCountryCode = \"US\" ", [values]);

The JSON I am referring to is

{
"laureates": [
{
"id": "1",
"firstname": "Wilhelm Conrad",
"surname": "Röntgen",
"born": "1845-03-27",
"died": "1923-02-10",
"bornCountry": "Prussia (now Germany)",
"bornCountryCode": "DE",
"bornCity": "Lennep (now Remscheid)",
"diedCountry": "Germany",
"diedCountryCode": "DE",
"diedCity": "Munich",
"gender": "male",
"prizes": [
{
"year": "1901",
"category": "physics",
"share": "1",
"motivation": "\"in recognition of the extraordinary services he has rendered by the discovery of the remarkable rays subsequently named after him\"",
"affiliations": [
{
"name": "Munich University",
"city": "Munich",
"country": "Germany"
}
]
}
]
},
{
"id": "996",
"firstname": "Robert",
"surname": "Wilson",
"born": "1937-05-16",
"died": "0000-00-00",
"bornCountry": "USA",
"bornCountryCode": "US",
"bornCity": "Geneva, NE",
"gender": "male",
"prizes": [
{
"year": "2020",
"category": "economics",
"share": "2",
"motivation": "\"for improvements to auction theory and inventions of new auction formats\"",
"affiliations": [
{
"name": "Stanford University",
"city": "Stanford, CA",
"country": "USA"
}
]
}
]
}
]
}

I have this JSON in an external file along with many entries. I couldn't find any docs to figure out.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vishwa.R
  • 11
  • 1

0 Answers0