I am trying to parse a csv file in node.js , i am able to parse the csv file and can print the content , the contents are coming as a from of a json object.Now my target is to iterate the json object and take out specific key and values from each block and use them in a Query which will do some DB operations.But the problem is while i am trying to iterate the json only first key and values of the first block is printed. Let me post the code what i have done
fs.createReadStream(path)
.pipe(csv.parse({headers:true ,ignoreEmpty : true}))
.on("error",(error) => {
throw error.message;
})
.on("data",function(data){
if(data && data!=={}){
Object.keys(data).forEach(function(k){
if(k==='name' || k==='Office'){
let selectQury = `select name,Office from myTable where name = ${data['name']} and Office
=${data[Office]};
db.query(selectQury,(err,res)=>{
if(err){
console.log('error',null);
This my json which i parse from the csv looks like
{
id:1,
name:"AS",
Office:"NJ"
........
ACTIVE: 1.
},
{
id:2,
name:"AKJS",
Office:"NK"
........
ACTIVE: 2.
}
so now what i want is in the select Query the parameters will be passed like
let selectQury = `select name,Office from myTable where name = "AS" and Office = "NJ";
in the first iteration
let selectQury = `select name,Office from myTable where name = "AKJS" and Office = "NK";
in the second iteration and so on as the csv grows.
I am not able to do it ,please help . Thanks in advance. I am new to node.js & tricky javascript operations.
I would like to expose you my problem: practically, through an AJAX function I call a server-side function, which should execute a query. This query, based on the data provided, deletes the last row of the table where the data in question is present. In the server side function everything works except the query, which instead of deleting the last row, deletes all the rows. Can anyone tell me the exact query?
public static void Contr_Dati(string provin)
{
string queryString = "DELETE FROM Prov_inser WHERE (SELECT MAX (Province_Inserite) FROM Prov_inser) = #Prov";
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["coso"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(queryString, connection))
{
SqlParameter parameter = new SqlParameter("Prov", provin);
command.Parameters.Add(parameter);
command.ExecuteNonQuery();
connection.Close();
}
}
}
If #Prov is the Max(key) of the table, your query
DELETE FROM Prov_inser WHERE (SELECT MAX (Province_Inserite) FROM Prov_inser) = #Prov
is equal to
DELETE FROM Prov_inser WHERE 1 = 1
which will delete all rows in the table
===================================================================
So you have to change it to
DELETE FROM Prov_inser WHERE Province_Inserite = #Prov
or without parameter
DELETE FROM Prov_inser WHERE Province_Inserite = (SELECT MAX(Province_Inserite) FROM Prov_inser)
If #Prov isn't a key, you have to add a key or a timestamp audit field (i.g. Created_Time) to identify the last inserted row of the table.
The solution that comes closest to the problem is: 'DELETE FROM Prov_inser WHERE Province_Inserite = Prov'. However #prov is not a key, but rather an input parameter. Prov_Inser is the name of the table and Province_Inserite is not the key.
I am trying to retrieve all the names of the tables in a DB in a particular schema but this is working only on information schema and returning null for others. Here is my attempt:
CREATE PROCEDURE tablename(DB VARCHAR,SC VARCHAR)
RETURNS array
LANGUAGE JAVASCRIPT
AS
$$
var t = [];
var stmt = snowflake.createStatement({
sqlText: "SELECT table_name FROM "+ DB +".information_schema.TABLES where TABLE_TYPE='BASE_TABLE';"});
var e = stmt.execute();
while(e.next())
{
var x = r.getColumnValue('table_name');
t.push(x);
}
return t;
$$;
information_schema.TABLES only displays objects for which the current role for the session has been granted access privileges.
Maybe you are missing some privileges and that's why you don't see your other tables but only the tables from information_schema?
To check that you can use SHOW GRANTS.
More infos: https://docs.snowflake.com/en/sql-reference/info-schema/tables.html
and https://docs.snowflake.com/en/sql-reference/sql/show-grants.html
Kind regards :-)
So I have set up a query builder that builds a query based on the users interaction with the data filtration area on the front end which contains a lot of radio buttons and dropdown boxes etc. Similar to what eBays data filtration function provided on their website.
My Query Builder so far:
app.post('/user/test',function(req, res) {
var query = {};
if (req.body.region){
query.region = req.body.region
console.log(query.region)
}
if(req.body.sector){
query.sector = req.body.sector
console.log(query.sector)
}
if(req.body.client){
query.client = req.body.client
console.log(query.client)
}
Project.find(query, function(err, project){
if (err){
res.send(err);
}
console.log(project);
res.json(project);
});
});
Now the above works very well. I can send filtration options in any scenario and it will bring back the required result. For example I can only send the region name and it will give me all the data that belongs to that region or I can send region name, sector name and it will further filter down the data that matches region and sector name sent and so on.
The Issue:
Now my database contains an array of data like:
words: ["book", "table", "pen"]
Each object in the database will have this array. So if there are 100 objects in the database each has one of these will have the "words" array with different or similar values.
I want to be able to send multiple options like "table" , "pen" to my database and get all the objects that contains the those two options within the data array.
To achieve that I did the following:
if (req.body.sol){
var arr = [];
arr.push(req.body.sol)
query.words = {words: {$in: arr}}
}
The above Did not work.
But if I make the following changes to this line:
From
query.words = {words: {$in: arr}}
to
query = {words: {$in: arr}}
Making the above change does work but then it does not build the remaining queries. It only builds the "$in" query.
Any idea how I can fix this?
you can simply write the query like
query.words = {$in: arr}
This way you would be able to build rest of the query.
the reason why query.words = {words: {$in: arr}} fails is that the query becomes{words:{words: {$in: arr}}}, which is not what you want, since its trying to find words inside words.
instead using query.words = {$in: arr} will make your query {words: {$in: arr}
You can use the bracket notation to add the $in operator in your query properties:
if (req.body.sol){
var arr = [],
obj = {};
arr.push(req.body.sol);
obj["$in"] = arr;
query.words = obj;
}
I am developing an application using phonegap and i am using database as sqlite.
I have created a table using following commands:
var dbb;
var shortName = 'Vaccine1';
var version = '2.0';
var displayName = 'vaccine';
var maxSize = 100000;
dbb = window.openDatabase(shortName, version, displayName,maxSize);
and inserted values using this function..
function AddDBvalues()
{
dbb.transaction(function(tx){
//tx.executeSql( 'DROP TABLE IF EXISTS Vaccin',nullHandler,nullHandler);
tx.executeSql( 'CREATE TABLE IF NOT EXISTS Vaccin(Vday INTEGER NOT NULL,VName TEXT NOT NULL, CCountryid INTEGER NOT NULL , Sex TEXT NOT NULL)', [],nullHandler,errorHandler);},errorHandler,successCallBack);
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["0","BCG","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["0","OPV dose 1 of 5","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["1","Hepatites B dose 1 of 2","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["42","DPT dose 1 of 3","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["42","OPV dose 2 of 5","91","both"], nullHandler,errorHandler);});
dbb.transaction(function(transaction) {transaction.executeSql('INSERT INTO Vaccin(Vday,VName,CCountryid,Sex) VALUES (?,?,?,?)',["70","DPT dose 2 of 3","91","both"], nullHandler,errorHandler);});
}
and used this function to get valuse from database..
function ShowValue()
{
var cnn = sessionStorage.getItem('cid');//getting from session
var cn=parseInt(cnn);
alert (cn); //always show correct value
dbb.transaction(
function (transaction) {
transaction.executeSql("SELECT * from Vaccin WHERE CCountryid='"+cn+"';",[], dataHandler, errorHandler);});
function dataHandler(transaction, results){
alert("first method" + results.rows.length);
for (var i=0; i<results.rows.length; i++) {
...... }
}}
i am getting an unexpected error is that the length of resultset increase every time
means if run app first time it show correct value and when i run it again it just show the length of resultset = results.rows.length+results.rows.length means double and so on....every time.
please help me if anybody know what's going wrong.
Is AddDBValues getting called on every run of the app? IF NOT EXISTS has no effect on the insert statements.
Is the database persistent between runs? If so then the data is doubling because not you're dropping the table. In you AddDBvalues() function the DROP ... command is commented out.
//tx.executeSql( 'DROP TABLE IF EXISTS Vaccin',nullHandler,nullHandler);
Unrelated but you also have a possible SQL injection vulnerability. The variable cn should be passed in as a bind variable and not simply added to the SQL as a string.
transaction.executeSql("SELECT * from Vaccin WHERE CCountryid='"+cn+"';",[], dataHandler, errorHandler);});