[FreeTDS][SQL Server]SQL Anywhere Error -265: Procedure 'SERVERPROPERTY' not found

[FreeTDS][SQL Server]SQL Anywhere Error -265: Procedure 'SERVERPROPERTY' not found

我正在尝试使用 pyodbc 和 FreeTDS ODBC 驱动程序连接到 SAP SQL Anywhere 数据库。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'mssql_database': {
        'ENGINE': 'django_pyodbc',
        'NAME': 'blabla',
        'USER': 'blabla',
        'PASSWORD': 'blabla',
        'HOST': '10.65.1.20',
        'PORT': '1433',
        'OPTIONS': {
            'driver': 'FreeTDS',
            'host_is_server': True,
        },
    },
    'sybase_database': {
        'ENGINE': 'django_pyodbc',
        'NAME': 'blabla',
        'USER': 'blabla',
        'PASSWORD': 'blabla',
        'HOST': '10.60.1.6',
        'PORT': '2638',
        'OPTIONS': {
            'driver': 'FreeTDS',
            'host_is_server': True,
        },
    },
}

连接到 mssql_database 有效。但是与 sybase_database 的连接以此错误消息结束: django.db.utils.DatabaseError: ('42000', "[42000] [FreeTDS][SQL Server]SQL Anywhere Error -265: Procedure 'SERVERPROPERTY' not found (504) (SQLExecDirectW)")

全部回溯

$ python manage.py inspectdb --database=sybase_database
Traceback (most recent call last):
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django_pyodbc/base.py", line 491, in execute
    return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]SQL Anywhere Error -265: Procedure 'SERVERPROPERTY' not found (504) (SQLExecDirectW)")

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/commands/inspectdb.py", line 25, in handle
    for line in self.handle_inspection(options):
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/core/management/commands/inspectdb.py", line 38, in handle_inspection
    with connection.cursor() as cursor:
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 162, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django_pyodbc/base.py", line 362, in _cursor
    if self.ops.sql_server_ver < 2005:
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django_pyodbc/operations.py", line 138, in _get_sql_server_ver
    cur.execute("SELECT CAST(SERVERPROPERTY('ProductVersion') as varchar)")
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/home/pd/packaging/venv/lib/python3.6/site-packages/django_pyodbc/base.py", line 497, in execute
    raise utils.DatabaseError(*e.args)
django.db.utils.DatabaseError: ('42000', "[42000] [FreeTDS][SQL Server]SQL Anywhere Error -265: Procedure 'SERVERPROPERTY' not found (504) (SQLExecDirectW)")

$ pip list
Package       Version
------------- -------
Django        1.8
django-pyodbc 1.1.3
pip           21.3.1
pyodbc        4.0.32
setuptools    59.6.0
sqlany-django 1.13
sqlanydb      1.0.11
wheel         0.37.1

当我运行这个脚本

# hello_sybase.py
import pyodbc

try:
    con = pyodbc.connect('Driver={FreeTDS};'
            'Server=10.60.1.6,2638;'
                      'Database=blabla;'
                      'uid=blabla;pwd=blabla')
    cur = con.cursor()
    cur.execute("Select * from Test")
    for row in cur.fetchall():
        print (row)
    cur.close()
    con.close()
except Exception as e:
    print(str(e))

它可以工作并打印所有行。是否至少可以在 django 模板中处理结果(行)?只读数据库就足够了。

根据 the site for django-pyodbc,不支持 Sybase。

回溯导致 this code,这似乎是 SQL 服务器特定的。您必须设法使该库与您的数据库兼容......但是既然您说您不一定需要 Django 的 ORM 东西,那么您绝对可以使用原始 pyodbc 层连接到辅助数据库, 例如

import pyodbc

# TODO: move this to `settings`?
CONN_STRING = 'Driver={FreeTDS};Server=10.60.1.6,2638;Database=blabla;uid=blabla;pwd=blabla'


def my_view(request):
    with pyodbc.connect(CONN_STRING) as conn:
        cur = conn.cursor()
        cur.execute('SELECT * FROM test')
        rows = list(cur.fetchall())
    return render(request, 'my_template.html', {'rows': rows})