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:
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,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
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./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,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
Share Improve this question asked Nov 17, 2017 at 16:11 CallmelifeCallmelife 511 gold badge1 silver badge5 bronze badges2 Answers
Reset to default 1result
returned by stmt.execute
is undefined, must be because err
is thrown, check for error in stmt.execute
's callback.
Also is there specific reason why you are using sync operations over asyn? If you have no constrains on making async calls, I would remend trying https://www.npmjs./package/tedious
Thanks for the input; I did eventually switch over to asyn (using db.openSync to establish the connection and db.prepareSycn).
The issue was arising from the usage of 'User' as a term - 'User' is reserved in the SQL database I'm connecting to. This issue was solved by add brackets to the term 'User' in the statement string.
The following code works:
let stmt = db.prepareSync('insert into Contact (FirstName, LastName, Country, [User], Email, PrivacyAgreement, PhoneNumber) VALUES (?,?,?,?,?,?,?)')
Thank you all!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745390707a4625649.html
评论列表(0条)