与Django一起作为辅助数据库的SAP SQL的连接问题

发布于 2025-01-30 05:52:36 字数 2308 浏览 4 评论 0原文

我可以使用freetdspyodbc连接到SAP SQL在任何数据库中,如下所示:

# 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视图中连接如下:

import pyodbc

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

当我运行Python Manage时。 py runserver并在上面的视图中运行代码。

我有这个错误消息 '08001','[08001] [freetds] [sql Server]无法连接到数据源(0)(sqldriverConnect)')

我试图将tds_version = 7.4放置如前所述在这里,在评论中,但没有帮助。

正如该评论所说的那样,这些是线程的问题吗?

我该如何修复?该代码在没有Django的情况下工作,但是使用Python Manage.py Runserver它不起作用。

更确切地说,我在视图中使用此代码

if second_form.is_valid():
            try:
                con = pyodbc.connect(CONN_STRING)
                con.setdecoding(pyodbc.SQL_CHAR, encoding='cp1252')
                con.setdecoding(pyodbc.SQL_WCHAR, encoding='cp1252')
                con.setencoding(encoding='cp1252')
                cur = con.cursor()
                cur.execute("Select * from test")
                result2 = list(cur.fetchall())
                print(results2)
                cur.close()
                con.close()
                context['result2'] = result2
                context['form2'] = SecondForm(request.POST)
            except Exception as e:
                print (str(e))

段源“ rel =“ nofollow noreferrer”>在这里是关于错误消息

由于以下一个或多个原因,将返回此SQLSTATE:

  DB2 ODBC无法与数据源建立连接。
连接请求被拒绝,因为已经存在使用嵌入式SQL建立的连接。
 

我的freetds版本是否有问题?如何在ubuntu 18.04上安全地升级它?

I can connect to a SAP SQL Anywhere database with FreeTDS and pyodbc as follows:

# 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))

I tried to connect in a Django view as follows:

import pyodbc

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

When I run python manage.py runserver and run the code in the above view.

I have this error message
'08001', '[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')

I tried to put TDS_Version=7.4 as was mentioned here in the comment, but it didn't helped.

Is it possible that these are issues with the threading as is said in that comment?

How can I fix it? The code works without Django, but with python manage.py runserver it doesn't work.

To be more precise, I use this code snippet in the view

if second_form.is_valid():
            try:
                con = pyodbc.connect(CONN_STRING)
                con.setdecoding(pyodbc.SQL_CHAR, encoding='cp1252')
                con.setdecoding(pyodbc.SQL_WCHAR, encoding='cp1252')
                con.setencoding(encoding='cp1252')
                cur = con.cursor()
                cur.execute("Select * from test")
                result2 = list(cur.fetchall())
                print(results2)
                cur.close()
                con.close()
                context['result2'] = result2
                context['form2'] = SecondForm(request.POST)
            except Exception as e:
                print (str(e))

Here is about the error message

This SQLSTATE is returned for one or more of the following reasons:

Db2 ODBC is not able to establish a connection with the data source.
The connection request is rejected because a connection that was established with embedded SQL already exists.

Is it problem with my FreeTDS version? How can I safely upgrade it on Ubuntu 18.04?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文