Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ODBC&;Node.js以插入数据_Javascript_Node.js_Odbc_Azure Sql Database_Sql Insert - Fatal编程技术网

Javascript 使用ODBC&;Node.js以插入数据

Javascript 使用ODBC&;Node.js以插入数据,javascript,node.js,odbc,azure-sql-database,sql-insert,Javascript,Node.js,Odbc,Azure Sql Database,Sql Insert,如有任何意见,将不胜感激 我使用ODBC连接字符串通过node.js连接到我的sql数据库。我可以成功地建立连接并查询数据库,但在尝试插入数据时遇到了问题 odbc插件可在以下位置找到: 下面是我尝试重新创建的示例: var db = require("odbc")() , cn = "DRIVER={FreeTDS};SERVER=host;UID=user;PWD=password;DATABASE=dbname" ; //Blocks until the connecti

如有任何意见,将不胜感激

我使用ODBC连接字符串通过node.js连接到我的sql数据库。我可以成功地建立连接并查询数据库,但在尝试插入数据时遇到了问题

odbc插件可在以下位置找到:

下面是我尝试重新创建的示例:

    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();
  });
})
这是我的密码:

 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, 'emailaccount@gmail.com', 1, '9999999999'], function (err, result) {
    result.closeSync();
    console.log(result);

    //Close the connection
    db.closeSync();
  });
})
注意:'User'和'PrivacyAgreement'是位数据类型(布尔值),其余是varchar

对此,我得到以下错误:

  ODBCStatement { queue: SimpleQueue { fifo: [], executing: true } }
/path/to/file/insert.js:22

 result.closeSync();

    ^

TypeError:无法读取stmt返回的未定义的

结果
的属性“closeSync”。execute未定义,必须是因为引发了错误,请检查
stmt.execute
的回调中是否有错误

您使用asyn上的同步操作还有具体原因吗?
如果您在进行异步调用方面没有限制,我建议您尝试

谢谢您的输入;我最终切换到了asyn(使用db.openSync建立连接和db.prepareSycn)

该问题是由于使用“User”作为术语而引起的—“User”保留在我连接到的SQL数据库中。通过在语句字符串中的术语“User”中添加括号,解决了此问题

以下代码起作用:

设stmt=db.prepareSync('insert-into-Contact(FirstName、LastName、Country、[用户]、Email、PrivacyAgreement、PhoneNumber)值(?,,,,,,,,,,,,?))

谢谢大家!