How to get sqlite database Values in javascript - javascript

i need how to get values from sqlite Database for particular columns records in Javascript, i successfully inserted values in DB but do no how to fetch the values from db
Here my sample code how i inserted :
var newPath1 = __dirname + path.sep+'schedule.sqlite'
var bfr = fs.readFileSync(newPath1)
var db = new sql.Database(bfr)
db.run("INSERT into
profile_id(SNo,visit_id,date,time,profile_url,First_Name,Last_Name) VALUES
(?,?,?,?,?,?,?)",
[result,result1,date1,time1,sampleUrl,first_Name,last_Name])
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync(newPath1, buffer);
i need a solution from this DB i need to fetch all values from First_Name

Try like this,
var stmt = db.prepare("INSERT into
profile_id(SNo,visit_id,date,time,profile_url,First_Name,Last_Name) VALUES
(?,?,?,?,?,?,?)");
stmt.run(result,result1,date1,time1,sampleUrl,first_Name,last_Name);
stmt.finalize();
db.each("SELECT First_Name FROM profile_id", function(err, row) {
console.log("First Name : "+row.First_Name);
});

finally i found the solution
var fName = db.exec("SELECT First_Name FROM profile_id")
this code works

Related

How to getting values ​with multiple key queries in IndexedDb

I created an ObjectStore named "components" with the code below.
var objectStore = db.createObjectStore("components", { keyPath: "id", autoIncrement: true });
let index = objectStore.createIndex('componentFloorId, componentRoomId', ['componentFloorId', 'componentRoomId']);
The view of the data in the browser
Using the componentFloorId and componentRoomId Keys, I can query with the code below.
const txn = db.transaction('components', 'readonly');
const store = txn.objectStore('components');
// get the index from the Object Store
const index = store.index('componentFloorId, componentRoomId');
// query by indexes
let query = index.getAll([floorIdFromCard, roomIdFromCard]);
However, what I need is to be able to query with the id key as well as these two keys.
SQL Query Example
SELECT * FROM components WHERE componentFloorId="1" AND componentRoomId="1" AND id=2;
How do I do this sql query with indexeddb?

How to insert json data into MariaDB using Nodejs?

I'm inserting JSON data into MariaDB using NodeJs. Getting below error while inserting data. Please advise what cause to get error. Actually Column data1 no empty or null values.Why am i getting below error ?
{ [Error: Column 'data1' cannot be null] code: 1048 }
Table Structure
CREATE TABLE `from_excel` (
`ID` INT(11) NOT NULL AUTO_INCREMENT,
`data1` VARCHAR(50) NULL DEFAULT NULL,
`data2` VARCHAR(100) NULL DEFAULT NULL,
PRIMARY KEY (`ID`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
Code which i'm using to insert data.
var Client = require('mariasql');
var c = new Client({
host : 'localhost',
user : 'xxxx',
password : 'xxxx',
db : 'Metrics'
});
const workbook = xlsx.readFile(__dirname + '/test.xlsx');
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
var json=xlsx.utils.sheet_to_json(worksheet);
console.log(json.length);
for(var i=0;i<json.length;i++)
{
var post = {data1: json[i].data1, data2: json[i].data2};
var sql = c.query('INSERT INTO elements_from_excel (data1,data2) VALUES (?,?)', post, function(err, result) {
console.log(sql);
if(err){console.log(err);}
else {console.log("success");}
});
}
c.end();
What could be happening is that the resulting insert statement being run is as follows:
INSERT into from_excel (data1, data2) VALUES (`data1` = \'data1value\', `data2` = \'value\', ?)
Try replacing the query string with the following instead:
var post = {data1: json[i].data1, data2: json[i].data2};
var sql = c.query('INSERT INTO from_excel SET ?', post, function(err, result) {
console.log(sql);
if(err){console.log(err);}
else {console.log("success");}
It should be INSERT INTO from_excel VALUES (?), although it's quite possible that you'll encounter other errors when you fix this one.
Make sure the function you are calling receive the exact type of data they expect.

Not able to get objectId in query.containedIn query with parse?

I am using parse.com as my back end.
I am using containedIn query to retrieve multiple user rows from parse "User" table.
My problem is that I am not able to get user's object Id in the success block
following is my code:
r_userIdList= ['PzpdRdKTVZ','PoJdRdKTVU','fvdRdKTVX'];
var query = new Parse.Query("_User");
query.containedIn("objectId", r_userIdList); // returns multiple rows
query.find({
success: function(r_uobject) {
var userObjId ,fName,lName,full_Name="Unknown User";
for(var i =0; i<r_uobject.length;i++){
var r_obj = r_uobject[i];
uID = r_obj.get('objectId');//not working
var u = uID.id;//not working
fName = r_obj.get('firstName');
lName = r_obj.get('lastName');
full_Name = firstName+' '+lastName;
r_userIdList[u].fullName = full_Name; //not working
}
}
});
Once I enter in the success block, I am unable to identify that which user's data has been retrieved.
Actually I need user's ObjectId because I have array called r_userIdList in which I need to store the firstName and LastName of user as Object.
IDs can usually be accessed with .id - in your case r_obj.id. Your uID.id won't work as uID is itself going to be undefined.
You also need to check that you are getting any results back at all: does r_userIdList have a length?

Firebase - Data structure issue for extracting an object from nested structure

Firebase - Data structure issue for extracting an object from nested structure.
I want to find the uid and then check if the key is a jobId.
I've labelled accordingly below.
I'm using typescript and angular2 with firebase.
This is my current attempt that returns "null":
var jobId = "-K5fIAiuHM-4xeEQJiIS";
var uid = "3f61ae7a-99a1-4cbf-9c8e-00b2249956a7";
var userRef = this.refApp.child('key').child(uid);
var query = userRef.child('jobId').child(jobId);
query.on('value', (snap) => {
//This returns null
var response = snap.val();
});
This is my database structure:
Your structure is /applications/$userId/$jobId. Use those keys to get to your data.
JSBin Demo
var jobId = "-K5fIAiuHM-4xeEQJiIS";
var uid = "3f61ae7a-99a1-4cbf-9c8e-00b2249956a7";
var refApp = new Firebase('<my-firebase-app>/applications');
var jobRef = refApp.child(uid).child(jobId);
jobRef.on('value', (snap) => console.log(snap.val()));
Right now you're using "key", which I believe is from my previous demo. That's just for show, not for your actual solution. Keep your data structure in mind when reading the sample code, because it can vary.

Javascript variables not working with MYSQL

I am trying to pull data from a table in MYSQL and save it to another table. Here is my code:
The two tables have the exact same columns. NOTE: The row does get saved but every value is apparently zero. Whenever I save this, MYSQL makes a new row with an index but all values are zero.
client.query('SELECT * FROM archive ORDER BY stamp LIMIT 1', function(err, result){
var nor = result[0].Northatt;
var eas = result[0].Eastatt;
var sou = result[0].Southatt;
var wes = result[0]. Westatt;
var time = result[0].stamp;
client.query("INSERT INTO quartly (Northatt, Eastatt, Southatt, Westatt, stamp) VALUES ('+nor+','+eas+','+sou+','+wes+','+time+')", function()err{
});
});
All of the variables are holding their respective 'int'...pretty sure I'm using the right syntax to save variables too.
Any help would be appreciated!
you have missed double quotes like that
VALUES ('" +nor+"','"+eas+"','"+sou+"','"+wes+"','"+time+"')
Escape parameters client side:
client.query('SELECT * FROM archive ORDER BY stamp LIMIT 1', function(err, result){
var nor = result[0].Northatt;
var eas = result[0].Eastatt;
var sou = result[0].Southatt;
var wes = result[0].Westatt;
var time = result[0].stamp;
client.query("INSERT INTO quartly (Northatt, Eastatt, Southatt, Westatt, stamp) VALUES (?,?,?,?,?)", [nor, eas, sou, wes, time], function()err{
});
});
Or use prepared statements with node-mysql2 (that way parameters are sent independently and not as part of sql query string)
client.query('SELECT * FROM archive ORDER BY stamp LIMIT 1', function(err, result){
var nor = result[0].Northatt;
var eas = result[0].Eastatt;
var sou = result[0].Southatt;
var wes = result[0].Westatt;
var time = result[0].stamp;
client.execute("INSERT INTO quartly (Northatt, Eastatt, Southatt, Westatt, stamp) VALUES (?,?,?,?,?)", [nor, eas, sou, wes, time], function()err{
});
});
Also you can do this in one query - see 'SQL Insert into … values ( SELECT … FROM … )' SO question

Categories

Resources