I am new to Python and trying to use the library pyodbc to connect to an MS Access Database. I have a 32 bit database, and 32 bit drivers. I keep reading but am unable to understand what looks like a simple set of commands.
import pyodbc
DBfile = 'C:/Users/davisr/My Documents/TEMP/Comp_Model_Db_Testing.mdb'
conn = pyodbc.connect("Driver={Microsoft Access Driver(*.mdb, *.accdb)};DBQ=DBfile")
The error that I received is as follows:
C:\Python27\python.exe C:/Users/davisr/PycharmProjects/File_Names/ex1.py
Traceback (most recent call last):
File "C:/Users/davisr/PycharmProjects/File_Names/ex1.py", line 6, in
conn = pyodbc.connect("Driver={Microsoft Access Driver(*.mdb, *.accdb)};DBQ=+DBfile")
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found
List item and no default driver specified (0) (SQLDriverConnect)')
Your help is appreciated. I am sure it is something simple.
Respectfully,
Robert Davis
I took the advice of beargle and Serge Ballesta and the following finally worked:
access_database_file = 'C:\\Users\\davisr\\My Documents\\TEMP\\Comp_Model_Db_Testing.mdb'
ODBC_CONN_STR = 'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' %access_database_file– user23208211 min ago
Thank you Serge and beargle
Related
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.
I have database connections inside a data pipeline. I am currently having issues using pyodbc.connect(cnxn_string) with an aws db-instance. While I can connect using pyodbc on local server. Moreover, the connection works when I use sqlalchemy.create_engine(cnxn_string).
example aws db-instance:
connection = DRIVER={ODBC Driver 17 for SQL Server};SERVER=mycompany-db.cu9fhgtuioo.us-east-1.rds.amazonaws.com;DATABASE=mycompanydatabase;UID=admin;PWD=foobar
example local server:
connection = DRIVER={ODBC Driver 17 for SQL Server};SERVER=LOCALCOMPANYSERVER-ANALYSIS\ROOT2005;DATABASE=mycompanydatabase;UID=admin;PWD=foobar
For now, instead of changing several of my packages to use sqlalchemy right away, I want to get this working and test the data pipeline. I want to use the pyodbc module to connect. I'm hoping there might some modification needed in the connection string to make pyodbc.connect work. Note, odbc drivers are installed, the aws instance allows connections (sqlalchemy works).
import pyodbc
pyodc.connect(cnxn_string)
#####
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open database "mycompanydatabase" requested by the login. The login failed. (4060) (SQLDriverConnect)')
It seems that you are missing quotations, the odd thing is that the
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open database "mycompanydatabase" requested by the login. The login failed. (4060) (SQLDriverConnect)')
is often related to SQL query code, and when a programming error occur, everything fine with that?
pyodbc.connect(
driver='{ODBC Driver 17 for SQL Server}',
system='=mycompany-db.cu9fhgtuioo.us-east-1.rds.amazonaws.com',
database='mycompanydatabase'
uid='admin',
pwd='foobar')
I hope I answer your question, if not. tell me and I will do my best to understand your question further.
Trying to connect access database to python so I can read it and eventually analyze it for certain things. But I can't connect to it and tried different approaches still getting the same error. I am using pyodbc.
Both python and access are 32bit. I am using windows 10 and the access is through MS office.
import pyodbc
connStr=(
r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};"
r"C:\Users\jonat\Desktop\Eritrean.accdb;"
)
cnxn=pyodbc.connect(connStr)
cursor=cnxn.cursor()
cursor.execute('select * from MemberPayment')
for row in cursor.fetchall():
print (row)
The error I've been getting
File "c:/Users/jonat/Desktop/pyth/prac.py", line 9, in <module>
cnxn=pyodbc.connect(connStr)
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect); [IM002] [Microsoft][ODBC Driver Manager] Invalid connection string attribute (0)')
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
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