You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Node.js odbc包查询数据库时遇SQLSetStmtOption错误及段错误求助

问题分析

你遇到的SQLSetStmtOption Option '5' is not supported错误,其中Option 5对应ODBC标准的SQL_QUERY_TIMEOUT(查询超时)选项。node-odbc包默认会尝试设置该选项,但你的Transoft ODBC驱动不支持此功能,后续的段错误是驱动返回错误后,包的错误处理逻辑引发的内存访问异常,解决前者后通常可消除崩溃问题。

解决方案

1. 显式禁用查询超时

在执行查询时,通过options参数将timeout设为0(表示不设置超时),避免node-odbc调用驱动不支持的选项。同时改用async/await风格适配AdonisJS的异步模式:

"use strict";

const odbc = require("odbc");

class OdbcController {
    async fetchData() {
        try {
            const connection = await odbc.connect('DSN=triad.udd;');
            // 执行查询时禁用超时设置
            const result = await connection.query(
                "Select Customer From m.POS_ARC_D_HEAD;",
                [],
                { timeout: 0 }
            );
            console.log('result', result);
            await connection.close();
        } catch (error) {
            console.error('查询错误:', error);
        }
    }
}

module.exports = OdbcController;

2. 强制使用ODBC 2.0兼容模式

如果驱动仅支持旧版ODBC规范,可在连接字符串中添加ODBCVER=0x0200,强制切换到ODBC 2.0模式,避免触发不兼容的3.0选项:

const connection = await odbc.connect('DSN=triad.udd;ODBCVER=0x0200;');

3. 升级node-odbc包

检查并升级到最新版本,新版本可能修复了旧驱动的兼容性问题:

npm update odbc

4. 手动预处理并执行语句

若自动查询方式仍有问题,尝试手动预处理语句,跳过默认的超时设置逻辑:

async fetchData() {
    try {
        const connection = await odbc.connect('DSN=triad.udd;');
        const stmt = await connection.prepare("Select Customer From m.POS_ARC_D_HEAD;");
        await stmt.execute();
        const result = await stmt.fetchAll();
        console.log('result', result);
        await stmt.close();
        await connection.close();
    } catch (error) {
        console.error('查询错误:', error);
    }
}

验证步骤

  1. 执行修改后的代码,确认是否还会出现SQLSetStmtOption Option '5' is not supported错误;
  2. 若错误消失,段崩溃问题通常会随之解决;
  3. 若仍有异常,查阅驱动文档确认支持的ODBC选项,排查是否有其他需禁用的设置。

内容的提问来源于stack exchange,提问作者Alek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 06:20:30