I'm trying to connect to MS Access database (.mdb) with pyodbc. The following connection string works properly:
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};'
r'DBQ=\\abc123.org\Team\Resources\Inputs\Accounts.mdb;')
I need to iterate over several files, so I'm trying to pass a variable in for the file path. Variables will come from a list but as an example:
file_path1 = '\\abc123.org\Team\Resources\Inputs\Accounts.mdb'
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ={};').format(file_path1)
I receive the following error:
Error: ('HY000', '[HY000] [Microsoft][ODBC Microsoft Access Driver] Not a valid file name. (-1044) (SQLDriverConnect); [HY000] [Microsoft][ODBC Microsoft Access Driver] Not a valid file name. (-1044)')
I receive the same error when trying:
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;') % file_path1
Would appreciate any help on how to pass the file path as a variable in the connection string, thank you!
I think I got it.
file_path1 = '\\abc123.org\Team\Resources\Inputs\Accounts.mdb'
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ={};'.format(file_path1))
#^ format method is call on the string
Can you spot the difference ?
What you where doing was to call format on the connect method.
You have the same issue on the 2nd try too
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' % file_path1)
#^ I have moved the parenthesis
Thank you #Paulo - I used your reply along with pathlib to resolve this issue:
import pathlib
file_path1 = pathlib.Path(r'\\abc123.org\Team\Resources\Inputs\Accounts.mdb')
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' % file_path1)
Related
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
I am trying to connect the Azure, SQL Server Database using Active Directory Password with python. But i got the below error.
Please check the below error:-
Traceback (most recent call last):
File "database_test.py", line 20, in <module>
main()
File "database_test.py", line 11, in main
connection = pyodbc.connect('DRIVER='+driver+';SERVER='+serverName+';PORT=1443;DATABASE='+dbName+';UID='+User_name+';PWD='+ password+';Authentication=ActiveDirectoryPassword')
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: The Microsoft Online Services Sign-In Assistant could not be found. Install it from http://go.microsoft.com/fwlink/?Link Id=234947. If it is already present, repair the installation. [2]. (2) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Client unable to establish connection (2); [08001] [Microsoft][ODBC Driver 13 for SQL Server]In
valid connection string attribute (0)')
Please Check the Below code:-
import pyodbc
def main():
serverName = "<ServerName>"
dbName = "<DatabaseName>"
User_name = '<UserName>'
password = '<Password>'
driver= '{ODBC Driver 13 for SQL Server}'
connection = pyodbc.connect('DRIVER='+driver+';SERVER='+serverName+';PORT=1443;DATABASE='+dbName+';UID='+User_name+';PWD='+password+';Authentication=ActiveDirectoryPassword')
cursor = connection.cursor()
data = cursor.execute("select * from dbo.test;")
allData = data.fetchall()
connection.close()
for i in allData:
print(i)
if __name__== "__main__":
main()
Is there any way to resolve the above problem?
Is it possible to connect azure sql server Database using pyodbc with Active Directory Password authentication? if possible what is the proper way to connect the Azure Sql Server Database with Active directory Password?
Please make you have installed the driver for Azure SQL database. You can download from this document Quickstart: Use Python to query an Azure SQL database.
This document can give more guides with Python.
And according you error message, you have missed "The Microsoft Online Services Sign-In Assistant". Please download and install it from the link provided for you:http://go.microsoft.com/fwlink/?Link Id=234947.
Here is my test Python code, I made some change that you can see more clearly:
import pyodbc
def main():
serverName = "****.database.windows.net"
dbName = "Mydatabase"
User_name = '****#****.com'
password = '****'
Authentication='ActiveDirectoryPassword'
driver= '{ODBC Driver 17 for SQL Server}'
connection = pyodbc.connect('DRIVER='+driver+
';Server='+serverName+
';PORT=1433;DATABASE='+dbName+
';UID='+User_name+
';PWD='+ password+
';Authentication='+Authentication)
cursor = connection.cursor()
data = cursor.execute("select * from dbo.tb1;")
allData = data.fetchall()
connection.close()
for i in allData:
print(i)
if __name__== "__main__":
main()
Note: I use ODBC Driver 17 for SQL Serve in my computer.
Hope this helps.
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 am writing a script that needs to make a connection to a microsoft access .accdb file and extract data from it.
Here is a snippet of what I am doing?
import pypyodbc
pypyodbc.lowercase = False
conn = pypyodbc.connect(
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' +
r'DBQ=file.accdb;'
)
cur = conn.cursor()
I get the following error:
raise Error(state,err_text)
pypyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified')
I have installed the drivers for ODBC as instructed in https://learn.microsoft.com/en-us/sql/connect/python/pyodbc/step-1-configure-development-environment-for-pyodbc-python-development
But the error persists, any idea what should I do or where I am going wrong?
I'm trying to connect to an oracle database with Pyodbc:
pyodbc.connect('{Microsoft ODBC for Oracle};Server=serverxzy.com:1234;Uid=myusername;Pwd=pass123')
I get the following error message:
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Any suggestions how to fix it would be appreciated. I'm specifically interested in getting pyodbc to work and NOT cx_Oracle.
You have to use the proprietary library for Oracle, cx_Oracle, and you must have the Oracle client and SDK installed.
Once this is all set up you can simply:
import cx_Oracle
conn_str = 'USER/PASS#HOSTNAME:PORT/ALIAS'
conn = cx_Oracle.connect(conn_str)
Then you can create a cursor with the conn object:
c = conn.cursor()
And then you can execute SQL:
c.execute(SQL)
Consider specifying the DRIVER in connection string:
pyodbc.connect('DRIVER={Microsoft ODBC for Oracle};Server=serverxzy.com:1234;
Uid=myusername;Pwd=pass123')