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
?