PYODBC--Data source name not found. How to get valid connection String - python

I am using SQL Server with Docker. I am trying to connect with pyodbc to my server but then I've got:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Using DBeaver I got some details:
SELECT
##servername AS 'Server Name',
##servicename AS 'Instance Name',
DB_NAME() AS 'Database Name',
HOST_NAME() AS 'Host Name'
and this SQL returns:
193aeb2132e4, MSSQLSERVER, model, HDZKNV2
How my connection string should looks like cause this one does not work (and I guess any variation of this)?
'DRIVER={Microsoft Access Text Driver};
SERVER=193aeb2132e4\MSSQLSERVER;
DATABASE=model;
Trusted_Connection=yes;
User=admin;
Password=passowrd'

Several issues:
MSSQLSERVER is the internal instance name for the default, unnamed instance of SQL Server - to connect to it, just use the server name (or IP address) without adding MSSQLSERVER as the instance name
You should not have Trusted_Connection=yes (use currently logged in user's credentials) along side with explicit username/password - use one or the other, but not both together.
And according to the official Microsoft documentation on PyODBC - you should use DRIVER={ODBC Driver 17 for SQL Server} and UID/PWD (not User)
So try these settings:
DRIVER={ODBC Driver 17 for SQL Server};
SERVER=193aeb2132e4;
DATABASE=model;
UID=admin;
PWD=passowrd

Related

How do I edit Azure SQL Database using pyodbc/odbc?

I wrote a Python program that web scraped a website and added the results to a Microsoft Access database. I now want to run the script again, with it adding the data to an Azure SQL database. I keep getting this error.
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.
I have tried t edit the settings of the database to no avail. Could someone tell me what settings to apply to the database? I also tried to see if there was a way to run the Python script inside azure to try to avoid the problem. Is this possible?
cnxn = pyodbc.connect(r'Driver={ODBC Driver 18 for SQL Server};Server=tcp:servername.database.windows.net,1433;Database=sizedb3;Uid={your_user_name};Pwd={your_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryPassword')
I tried this driver. I have downloaded the driver from Microsoft's website. This driver is a connection string in the ODBC section of the Azure SQL database in the Azure portal.
I tried running the below python code to connect to Azure SQL DB with Azure AD authentication.
Code:-
import pyodbc
conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server=tcp:siliconserver.database.windows.net,1433;'
'Database=silicondb;'
'Uid=xxxser#sid24desaioutlook.onmicrosoft.com;'
'Pwd=xxxxxxxxxx#123;'
'authentication=ActiveDirectoryPassword')
cursor = conn.cursor()
cursor.execute('SELECT * FROM StudentReviews')
for i in cursor:
print(i)
cursor.close()
conn.close()
Output:-
Make sure you have allowed your Client IP in your Azure SQL server Networking tab like below:-
I tried to remove one syntax/spelling from my Azure SQL connection string and got the same error code as yours like below:-
You can validate your connection string server spelling and syntax from your Azure SQL server DB > Connection String > like below:-
Also, Make sure you have added your client IP and allowed it in your Azure SQL Server like below:-

Trying to connect to IBMi Series DB2 database to use python to work with databases [duplicate]

im trying to make a connection to an as400 with db2 using pyodbc and the ibm db2 odbc driver.
import pyodbc
connection = pyodbc.connect(
driver='{IBM DB2 ODBC DRIVER}',
system='192.168.1.100',
uid='user',
pwd='pass')
c1 = connection.cursor()
#this is meaningless sql, i just want the connection
c1.execute('select * from libname.filename')
for row in c1:
print (row)
Running this gives me this error
python pydata.py
Traceback (most recent call last):
File "C:\Users\tca\Desktop\ScriptingSTuff\pydata.py", line 3, in <module>
connection = pyodbc.connect(
pyodbc.OperationalError: ('08001', '[08001] [IBM][CLI Driver] SQL1013N The database alias name or database name "" could not be found. SQLSTATE=42705\r\n (-1013) (SQLDriverConnect)')
Any ideas?
This is all under win10
EDIT:
Adding this "database='s10c38ft',"
import pyodbc
connection = pyodbc.connect(
driver='{IBM DB2 ODBC DRIVER}',
system='192.168.1.100,8471',
database='s10c38ft',
uid='user',
pwd='pass')
c1 = connection.cursor()
c1.execute('select * from libname.filename')
for row in c1:
print (row)
Makes it hang on a blinking cursor, I cant even CTRL+C to end it, I have to close cmd.
The proper driver name should be IBM i Access ODBC Driver (but see notes below). Other than that, your first example was correct:
connection = pyodbc.connect(
driver='{IBM i Access ODBC Driver}',
system='192.168.1.100',
uid='user',
pwd='pass')
If that doesn't work, there are two main possibilities:
You are using an old ODBC driver. This would happen if you are using the old iSeries Access (in which case the driver name is iSeries Access ODBC Driver) or even older Client Access (driver name Client Access ODBC Driver (32-bit)). If you choose the appropriate name for your driver, it will work.
You are using an ODBC driver that is not for IBM i. The most commonly used member of the Db2 family is Db2 for LUW (Linux, Unix, Windows), but there are others. None of these will work for you.
You can find out the list of exact ODBC driver names you have installed by calling pyodbc.drivers(). If you don't have any of the ones I mentioned above by name, then you don't have the right driver. The ODBC driver you want is the one described here.

What can cause pyodbc to connect to one SQL Server and not another?

I am trying to connect to SQL Server using pyodbc to query some tables for work. I am able to connect to one of our servers and run a SQL query, but the same code does not work for a different server. The error message I get is this:
Error: ('008001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]
SQL Server does not exist or access denied. (17) (SQLDriverConnect)')
This is the code that I'm using:
import pyodbc
import pandas as pd
conn = pyodbc.connect('Driver={SQL Server};'
'Server=servername;'
'Database=databasename;'
'Trusted_Connection=yes;')
[rest of code]
The code works for one server, but when I change the server & database names to the other, I get the error message (there are no spelling errors in the server/database names). I am able to access this server through SQL. Additionally, I am able to access this server and to query/update its tables using Excel VBA (even though it will not connect in Python).
Based on my research, there are many possible issues that I do not fully understand. I have tried changing the driver name in the code, and that failed. Since this code is for work, there are security permissions that prevent me from trying out certain solutions.
What could be causing this issue? If somebody could point me in the right direction, that would help me ask better questions of our IT team, if I'm not able to solve it myself.
(I'm using Microsoft SQL Server Management Studio 17 and Python 3.6 with Spyder.)
Try with this driver:
SQLserver_settings = {'server': 'servername', 'database': 'database', 'username': 'username', 'password': 'password', 'driver': '{ODBC Driver 17 for SQL Server}'}
cnxn = pyodbc.connect('DRIVER=' + SQLserver_settings['driver'] + ';SERVER=' + SQLserver_settings['server'] + ';PORT=1433;DATABASE=' + SQLserver_settings['database'] + ';UID=' + SQLserver_settings['username'] +';PWD=' + SQLserver_settings['password'])

How can i connect to local advantage database using pyodbc in python?

As far as i can understand to use pyodbc you have to
cnxn = pyodbc.connect('DRIVER={Advantage ODBC Driver};SERVER=local;DataDirectory=\\AltaDemo\Demo\AltaPoint.add;DATABASE=AltaPoint;UID=admin;PWD=admin;ServerTypes=1;')
cursor = cnxn.cursor()
This is the error i get from the console when i run this
Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
The name of the driver is Advantage StreamlineSQL ODBC, so the bare minimum connect string is:
DRIVER={Advantage StreamlineSQL ODBC};DataDirectory=D:\Temp
Other optinal options are:
DefaultType=Advantage
User ID=xxx
Password=xxx
ServerTypes=2
AdvantageLocking=ON
CharSet=ANSI
Language=ANSI
Description=My ADS connection
Locking=Record
MaxTableCloseCache=25
MemoBlockSize=64
Rows=False
Compression=Internet
CommType=TCP_IP
TLSCertificate=
TLSCommonName=
TLSCiphers=
DDPassword=Dictionary Password
EncryptionType=
FIPS=False
TrimTrailingSpaces=True
SQLTimeout=600
RightsChecking=OFF
If you want to use the Local Server you have to pass ServerTypes=1 like you already have in your original string.
For more options and documentation see also:
The official documentation
ConnectionStrings.com

connect to Azure SQL database via pyodbc

I use pyodbc to connect to my local SQL database which works withoout problems.
SQLSERVERLOCAL='Driver={SQL Server Native Client 11.0};Server=(localdb)\\v11.0;integrated security = true;DATABASE=eodba;'
cnxn = pyodbc.connect(SQLSERVERLOCAL) #works
I try the connection to the azure sql database with:
SQLSERVERAZURE='Driver={SQL Server Native Client 10.0};Server=tcp:mydatbase.database.windows.net,1433;Database=mydb;Uid=myuser#myerver;Pwd=mypass;Encrypt=yes;Connection Timeout=30;'
cnxn = pyodbc.connect(SQLSERVERAZURE) #works not
what gives me the error:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Besides the suggestions that provided by meet-bhagdev who recommended to use pymssql dirve that mentioned in link, to resolve the error: Data source name not found and no default driver specified (0) (SQLDriverConnect)') that encountered, please update your connect string as below to see if it works.
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=yoursqlAzureServer.database.windows.net,1433', user='yourName#yoursqlAzureServer', password='Password', database='DBName')
Download ODBC driver from Microsoft website and try installing it. I guess then the problem should disappear.
Here is the link:
https://www.microsoft.com/en-us/download/details.aspx?id=50420

Categories