0

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?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
xralf
  • 3,312
  • 45
  • 129
  • 200
  • https://stackoverflow.com/questions/72255279/freetdssql-serversql-anywhere-error-265-procedure-serverproperty-not-fou#comment127661179_72255279 – Gord Thompson May 17 '22 at 12:48
  • From a stand-alone Python script or an interactive Python shell, create a connection to your database with `cnxn = pyodbc.connect(…)` and then `print(cnxn.getinfo(pyodbc.SQL_DBMS_NAME))`. What does it say? – Gord Thompson May 17 '22 at 14:40
  • It helped to solve it by setting TDS_Version to 5.0 in connection string. – xralf May 17 '22 at 14:52
  • My question still stands. What does `cnxn.getinfo(pyodbc.SQL_DBMS_NAME)` say about the database you are *actually using*? – Gord Thompson May 17 '22 at 14:53
  • It prints `SQL Anywhere` – xralf May 17 '22 at 15:00

0 Answers0