2017-11-17 14 views
0

모든 입력은 인정 될 것입니다.ODBC & Node.js를 사용하여 데이터 입력

나는 ODBC 연결 문자열을 사용하여 node.js.s와 sql 데이터베이스를 연결하고 있습니다. 성공적으로 연결을 설정하고 데이터베이스를 쿼리 할 수 ​​있지만 데이터를 삽입 할 때 문제가 발생합니다.

는 ODBC 플러그인

는 여기에서 찾을 수 있습니다 :

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(); 
    }); 
}) 

참고 : 여기에

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(); 
    }); 
}) 

내 코드입니다 : 여기

https://www.npmjs.com/package/odbc 내가 다시 노력하고있어 예입니다 ' User '및'PrivacyAgreement '는 비트 데이터 유형 (부울)이고 나머지는 varchar입니다.

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

result.closeSync(); 

    ^

형식 오류 :

이있는, 나는 다음과 같은 오류 얻으려면 err가 발생하기 때문에 stmt.execute에 의해 반환 정의되지 않은

답변

1

result의 특성 'closesync 닫을 때'를 읽을 수 없습니다 것은 정의되어 있지를 확인해야합니다 stmt.execute 님의 콜백 오류입니다.

asyn을 통해 동기화 작업을 사용하는 특별한 이유가 있습니까? 비동기 호출에 제한이 없다면 시도하는 것이 좋습니다. https://www.npmjs.com/package/tedious

0

감사합니다. 결국 asyn (db.openSync를 사용하여 연결을 설정하고 db.prepareSycn을 사용)으로 전환했습니다.

용어는 '사용자'의 사용으로 인해 발생했습니다. '사용자'는 연결된 SQL 데이터베이스에 예약되어 있습니다. 이 문제는 명령문 문자열에서 '사용자'라는 용어에 대괄호를 추가하여 해결되었습니다.

다음 코드는 작동 :

하자 STMT = db.prepareSync는 ('연락처 (이름, 성, 나라에 삽입, [사용자], 이메일, PrivacyAgreement, PHONENUMBER) VALUES (,,,???? ,?,?,?) ')

감사합니다.