any input would be appreciated.
I'm using an ODBC connection string to connect to my sql database with node.js. I can successfully establish a connection and query the database, but I'm running into trouble when trying to insert data.
The odbc plugin can be found here: https://www.npmjs.com/package/odbc
Here is the example I'm trying to recreate:
var db = require("odbc")()
, cn = "DRIVER={FreeTDS};SERVER=host;UID=user;PWD=password;DATABASE=dbname"
;
//Blocks until the connection is open
db.openSync(cn);
db.prepare("insert into hits (col1, col2) VALUES (?, ?)", function (err, stmt) {
if (err) {
//could not prepare for some reason
console.log(err);
return db.closeSync();
}
//Bind and Execute the statment asynchronously
stmt.execute(['something', 42], function (err, result) {
result.closeSync();
//Close the connection
db.closeSync();
});
})
Here is my code:
var db = require("odbc")()
, cn = "Driver={ODBC Driver 13 for SQL Server};Server=host:insert-name.database.windows.net,insert-port;Database=insert-database-name;Uid=insert-uid;Pwd=insert-password;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
;
//Blocks until the connection is open
//Blocks until the connection is open
db.openSync(cn);
//Blocks while preparing the statement
db.prepare("INSERT INTO Contact (FirstName, LastName, Country, User, Email, PrivacyAgreement, PhoneNumber) VALUES (?,?,?,?,?,?,?)", function (err, stmt) {
if (err) {
//could not prepare for some reason
console.log(err);
return db.closeSync();
}
console.log(stmt);
//Bind and Execute the statment asynchronously
stmt.execute(['first','last','country_name', 1, '[email protected]', 1, '9999999999'], function (err, result) {
result.closeSync();
console.log(result);
//Close the connection
db.closeSync();
});
})
Note: 'User' and 'PrivacyAgreement' are bit datatype (boolean) and the rest are varchar.
To which, I get the following error:
ODBCStatement { queue: SimpleQueue { fifo: [], executing: true } }
/path/to/file/insert.js:22
result.closeSync();
^
TypeError: Cannot read property 'closeSync' of undefined