I am trying to access a microsoft sql server DB trough python but I am receiving Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified')
The only info that I have is the db ip, user, password and db name. I used dbevear to set the connection and it's ok but I need to get data with python.
I tried the following:
import pypyodbc
import pandas as pd
cnxn = pypyodbc.connect(
"Server=171.11.111.11;"
"Database=database_name;"
"uid=my_username;pwd=secret")
df = pd.read_sql_query('select * from db.dtable', cnxn)
I am quite new to python and I don't exactly how to connect to the db. Am I missing something here?
This
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified'
means you failed to specify which ODBC driver you want to use. See eg
import pyodbc
# Some other example server values are
# server = 'localhost\sqlexpress' # for a named instance
# server = 'myserver,port' # to specify an alternate port
server = 'tcp:myserver.database.windows.net'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
Connecting to SQL Server using pyodbc
Related
I am trying to connect to external MS SQL database server from our Linux server using pyodbc and I am getting error.
Code:
import pyodbc
server = 'XXXXXXXXX,port'
database = 'XXXXXXXXXXXXXXX'
username = 'XXXXXXXXXXXX'
password = 'XXXXXXXX'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
#Sample select query
cursor.execute("SELECT ##version;")
row = cursor.fetchone()
while row:
print(row[0])
row = cursor.fetchone()
cnxn.close()
Error:
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open database "xxxxxxxxxx" requested by the login. The login failed. (4060) (SQLDriverConnect)')
I also tried reading multiple solutions and trying them for example:
putting password in brackets like --> password = '{XXXXXXXX}' becuase it had special characters.
Also tried giving server as --> server = 'tcp:XXXXXXXXX,port' nothing worked it all gives me same error.
Could you please guide me resolve this issue.
Many thanks for your help.
I have already looked up a lot of some what similar questions on StackOverflow regarding the connectivity of MSSQL with pyodbc, but none of their solutions helped.
I'm tring to connect a MSSQL database which is lying on a VM server, and I'm trying to access it from my local system. Following is the code:
import pyodbc
server = '172.xxx.xxx.xxx,1443'
database = 'sample_db'
username = 'SA'
password = 'xxxxx'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password+';Trusted_Connection=yes')
cursor = cnxn.cursor()
cursor.execute("SELECT name FROM sys.databases;")
results = cursor.fetchall()
print(results)
The error I'm getting is as follows:
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
If there's any other alternative to pyodbc that works better with MSSQL, please do mention that too.
Any help is appreciated, thanks.
Edit:
Upon #GordThompson suggestion I checked the driver in pyodbc.drivers() and found that my system only has 'SQL Server' driver so I changed the Driver to SQL Server. The present code looks like this:
import pyodbc
server = '172.xxx.xxx.xxx,1443'
database = 'sample_db'
username = 'SA'
password = 'xxxxx'
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password+';Trusted_Connection=yes')
cursor = cnxn.cursor()
cursor.execute("SELECT name FROM sys.databases;")
results = cursor.fetchall()
print(results)
But now i'm getting a totally different error, still not sure what it is
Error:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53)')
Its important to not mention the Trusted_Connection parameter when logging-in using UID and PWD. Hence, when I removed the Trusted_Connection parameter, it was able to establish the connection successfully.
So the connection string that later worked for me just had these,
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
cursor = cnxn.cursor()
[ Since #GordThompson didn't post it as an answer, I'm positing it here and closing it. Thanks #GordThompson ]
I want to write a python dataframe (df_EVENT5_13) to SQL Server version 17 using SQL Alchemy. In order to do this I have to make an "engine" connection.
Let's say hypothetically below are the credentials I have for SQL Server:
Driver = {SQL Server}
Server = ABCD14GPBVTSI88\ABCDRD22,4421
Database = DB4
uid = abcde
pwd = 12345678
Below is the code I'm trying to run and the corresponding error:
import sqlalchemy as sa
import pyodbc
engine = sa.create_engine('mssql+pyodbc://abcde:12345678#ABCD14GPBVTSI88\ABCDRD22,4421/DB4')
df_EVENT5_13.to_sql("MOD_test", engine)
Below is the error I get:
DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I'm trying to do access the remote MS-SQL database from my OS X machine, with help of pyodbc and python. When I try to execute, it's showing an error like this
Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
The code I've written is:
import pyodbc
def createdatabase():
driver = 'SQL Server'
server = '00000000'
db1 = 'Vodafone'
tcon = 'yes'
uname = 'user'
pword = 'pwd'
cnxn = pyodbc.connect(driver='{SQL Server}', host=server,database=db1,trusted_connection=tcon, user=uname, password=pword)
cursor = cnxn.cursor()
This may not be your only problem, but the pyodbc.connect() string needs to be formed with semicolons like this:
DRIVER={FreeTDS};SERVER=yoursqlserver.com;PORT=1433;DATABASE=yourdb;UID=youruser;PWD=yourpass;TDS_Version=7.2;
You'll also have to read up on setting up FreeTDS and unixODBC from a Mac. If you're specifying SQL Server instead of FreeTDS, I'm guessing you haven't set up your configuration in freetds.conf, odbc.ini and odbcinst.ini, which are required for connecting to SQL Server from non-Windows machines.
I've read all the faq pages from the python odbc library as well as other examples and managed to connect to the DSN, using the following code:
cnxn = pyodbc.connect("DSN=DSNNAME")
cursor = cnxn.cursor()
cursor.tables()
rows = cursor.fetchall()
for row in rows:
print row.table_name
but for everything else I keep getting this error:
Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I know that I can pull up my data using Microsoft Access by going through the following steps: Creating a new database, clicking the external data tab, Click More and select ODBC database, use the Link to the data source by creating a linked table, in the Select data source window choosing Machine Data source and select NAME2 which has a System type, press okay and choose the table acr.Table_one_hh, then select the fields in the table that I want to look at like City, State, Country, Region, etc. When I hover over the table name it shows the DSN name, Description, Trusted Connection = Yes, APP, Database name and the table name.
I've attempted two methods, first
cnxn = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=mycomputername;DATABASE=mydatabase;Trusted_Connection=yes;')
cursor = cnxn.cursor()
which gives an error:
Error: ('08001', '[08001] [Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (SQLDriverConnect); [HYT00] [Microsoft][SQL Server Native Client 10.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 10.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. (2)')
I tried
cnxn = pyodbc.connect("DSN=DSNNAME, DATABASE=mydatabase")
cursor = cnxn.cursor()
cursor.execute("""SELECT 1 AS "test column1" from acr.Table_one_hh""")
cursor.tables()
rows = cursor.fetchall()
for row in rows:
print row.table_name
which gave an error
Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I managed to solve my issue. My code did not really change.
cnxn = pyodbc.connect("DSN=BCTHEAT")
cursor = cnxn.cursor()
cursor.execute("select * from acr.Table_one_hh")
row = cursor.fetchall()
then I wrote the results into a csv file.