Python vs Airflow pymssql - python

When I try to connect to a database using Airflow I get the error
'pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist'
What is strange I can connect using the exact same code in jupyter/python.
For reference this is a sample code of how I connect.
conn =
pymssql.connect(
server='server',
user='python_script',
password='password',
database='test_data',)
Any help if appreciated.

Related

(20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (127.0.0.1,4096)\n')

I tried connecting to a SQL Server database and the following errors occurred.
(20009, b'DB-Lib error message 20009, severity 9:
Unable to connect: Adaptive Server is unavailable or does not exist (127.0.0.1,4096)
Checks I made:
TTP/IP enabled for port 4096
Tried both named pipes enabled and disabled
Security>login>AA Server roles dbcreator.
Enabled Remote connection
import pymssql
conn = pymssql.connect(
host=r'127.0.0.1,4096',
user=r'AA',
password=r'BBBBBBBBBBBB',
database='table'
)
cursor = conn.cursor(as_dict=True)
cursor.execute("SELECT ##version;")
row = cursor.fetchone()
while row:
print(row\[0\])
row = cursor.fetchone()

Unable to connect to SQL server using PYMSSQL

I encounter this problem when trying to connect to my SQL database:
OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (LAPTOP-B3TE2LKC)\nDB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (LAPTOP-B3TE2LKC)\n')
I am able to connect to my database with the very same credentials using PYODBC, so the access variables should be fine.
Does anyone know how to solve this error message? - I am using Python version 3.8.8 and PYMSSQL version 2.2.2.
This is my code, including the credentials I used to connect to this test database:
import pymssql
print(pymssql.__version__)
# Database credentials and variables
server = 'LAPTOP-B3TE2LKC\TESTEXPRESS'
user = 'FTP-user'
password = '1111'
database = 'TestA'
# Connect to SQL database
database_connection = pymssql.connect(server, user, password, database)
Any help is highly appreciated.

I have a connexion problem to sql server using sqlalchemy wih python

I want to connect into a sql server database using sqlalchemy and python.
but when I run the code below I get the following error
OperationalError: (pyodbc.OperationalError) ('08001', '[08001]
[Microsoft][SQL Server Native Client 11.0]SQL Server Network
Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
(-1) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client
11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred
while establishing a connection to SQL Server. Server is not found or
not accessible. Check if instance name is correct and if SQL Server is
configured to allow remote connections. For more information see SQL
Server Books Online. (-1)') (Background on this error at:
http://sqlalche.me/e/e3q8)
I think the problem has something to do with the driver, but I can't seem to understand it.
here is my code:
from sqlalchemy.sql import text
from sqlalchemy import create_engine
import secrets
engine = create_engine( 'mssql+pyodbc://servername/test_db? driver=SQL+Server+Native+Client+11.0')
conn = engine.connect()
s = text("SELECT * FROM user_tab ")
result = conn.execute(s).fetchall()
print(result)
Can you help me please!
You're connecting to a DB without a user, password, hostname or port.
When you look at the example in the documentation this is written:
# pymssql
engine = create_engine('mssql+pymssql://scott:tiger#hostname:port/dbname')
As you can see, this url contains way more information than you have in your url.
You just got an url with a name and a dbname. I don't know what the first name represents..
Most of the time the hostname will be localhost and the port 1434. But make sure where your mssqlserver is running and which user is allowed to make queries on the db!
Documentation: https://docs.sqlalchemy.org/en/13/core/engines.html

Microsoft SQL Server Won't Connect Through Python

I've looked through some other things but haven't been able to find a working solution.
Here is my code:
conn = db.connect("Driver={SQL Server}; Server='Server';Database='Database_DW'; uid='uid'; pwd = 'pwd'")
I run this code and I get the following error:
DatabaseError: ('08001', '[08001] [Microsoft][ODBC SQL Server
Driver][DBNETLIB]SQL Server does not exist or access denied.')
I'm really at a loss here. I can log in fine through the SQL Server Client with the exact some credentials.
Consider adjusting connection strings as parameter values are not quoted. Right now, pypyodbc is attempted to find the 'Server' (quotes included) server.
conn = pypyodbc.connect("DRIVER={SQL Server};server=servername;database=databasename;" + \
"UID=username;PWD=***")
Alternatively, use keyword arguments:
conn = pypyodbc.connect(driver="{SQL Server}", host="servername", database="database",
uid="username", pwd="***")

Connect to SQL Server using python from Raspberry pi

I am trying to connect to a SQL Server database using python.I have followed,
http://blog.tryolabs.com/2012/06/25/connecting-sql-server-database-python-under-ubuntu/
I have used following Python code to connect with the Microsoft SQL Server Management Studio 2014 with above setting.
import pyodbc
user='sa'
password='PC#1234'
database='climate'
port='1433'
TDS_Version='8.0'
server='192.168.1.146'
con_string= 'UID=%s;PWD=%s;DATABASE=%s;PORT=%s;TDS=%s;SERVER=%s;' %
(user,password, database,port,TDS_Version,server)
cnxn=pyodbc.connect(con_string)
cursor=cnxn.cursor()
cursor.execute("select * from mytable")
row=cursor.fetchone()
print row
I got following error,
Traceback (most recent call last):
File "sql.py", line 15, in <module>
cnxn=pyodbc.connect(con_string)
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source
name not found, and no default driver specified (0) (SQLDriverConnect)')
I also have installed pymssql and tried to connect to SQL Server. For this I have used following python code,
import pymssql
connection=pymssql.connect(user='sa',password='PC#1234',
host='192.168.1.146',database='climate',as_dict=True)
cursor=connection.cursor()
cursor.execute('select * from mytable;')
rows=cursor.fetchall()
I have got following error,
connection=pymssql.connect(user='sa',password='PC#1234',
host='192.168.1.146',database='climate',as_dict=True)
File "/usr/lib/pymodules/python2.7/pymssql.py", line 607, in connect
raise OperationalError, e[0]
pymssql.OperationalError: DB-Lib error message 20009, severity 9:
Unable to connect: Adaptive Server is unavailable or does not exist
Net-Lib error during Operation now in progress Error 115
- Operation now in progress
what is the reason for showing data source name not found and adaptive server is not available?

Categories

Resources