How to query azure SQL from node.js server -
i'm having trouble querying db on azure sql (i new sql). i'm following steps on https://docs.microsoft.com/en-us/azure/sql-database/sql-database-connect-query-nodejs, includes tutorial steps on how read tables, not manipulate them. trying insert , delete requests on node.js server, getting request error in 1 of node modules, makes me think i'm going requesting operations wrong.
here's code:
var connection = require('tedious').connection; var request = require('tedious').request; // create connection database var config = { username: 'user_name', password: 'password', server: 'server_name', options: { database: '_dbname' , encrypt: true } } var connection = new connection(config); // attempt connect , execute queries if connection goes through connection.on('connect', function (err) { if (err) { console.log(err) } else { querydatabase(); } } //this works fine, it's loading data database function querydatabase() { console.log('\nreading rows table...'); let obj = {}; let objs = []; let request; // read rows table request = new request( "select * [dbo].[table_name]", function (err, rowcount, rows) { console.log('-- done'); } ); //this when admin adds content app, sql table not changing, //node.js throws error socket.on('add item', item => { let index = getcollectionindexbyid(item.id); collections[index].items.push(item.item); io.sockets.emit('add item', item); request = new request(`insert [dbo].[table_name](id, attr1, attr2, attr3, attr4) values (` + item.id + ',' + item.item.attr2 + ',' + item.item.attr3 + ',' + item.item.attr4 + ',' + null, function (err, rowcount, rows) { if (err) throw err; console.log('> requested db insert item'); }); connection.execsql(request); console.log('> item sent app'); }); //for when admin removes content app, same error socket.on('rm item', item => { collections[getcollectionindexbyid(item.id)].items.splice(item.index, 1); io.sockets.emit('rm item', { "id": item.id, index: item.index }); request = new request(`delete [dbo].[table_name] id= ` + item.id + ` , attr1= ` + item.item.attr1, function (err, rowcount, rows) { if (err) throw err; console.log('> requested db remove item') }); connection.execsql(request); console.log('> sent request remove item'); });
the exact error msg requesterror: incorrect syntax near 'esse'.
, , in 1 of node modules called tedious
in request.js
.
so in summary, if knows of way query azure sql db's make inserts , deletes, appreciated!
you missed )
in 'insert' sql. also, you'd need set string value single quotes of course.
change following lines of code
request = new request(`insert [dbo].[table_name](id, attr1, attr2, attr3, attr4) values (` + item.id + ',' + item.item.attr2 + ',' + item.item.attr3 + ',' + item.item.attr4 + ',' + null, function (err, rowcount, rows) { if (err) throw err; console.log('> requested db insert item'); }); connection.execsql(request);
to
request = new request(`insert [dbo].[table_name](id, attr1, attr2, attr3, attr4) values ('${item.id}', '${item.item.attr2}', '${item.item.attr3}', '${item.item.attr4}' null)`, function (err, rowcount, rows) { if (err) throw err; console.log('> requested db insert item'); }); connection.execsql(request);
Comments
Post a Comment