Connect to MS access database using PyQt5 - python

I'm trying to connect to a MS access database using the following code:
from PyQt5.QtSql import QSqlDatabase
db = QSqlDatabase.addDatabase("QODBC")
db.setDatabaseName(
"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\\database.accdb")
if db.open(): print("opened")
I have office 2019 installed.
I cannot figure out what is wrong in the connection string or the driver.

I figured out that this problem happened because I have a 64-bit of python and a 32-bit of access database engine driver.
After installing the 64-bit access database engine driver, I managed to get it work.

Related

Try to connect a Firebird 3.05 Database to Python (pandas)

I am trying to connect a Firebird database with Python. I already tried it with pyodbc:
import os
import pyodbc
server = '127.0.0.1/3050'
database = 'Databse-Name'
username = 'Username'
password = 'password'
cnxn = pyodbc.connect('DRIVER={Firebird/InterBase(r)
driver};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
I get this error:
OperationalError: ('08004', "[08004] [ODBC Firebird Driver]Unable to connect to data source: library 'gds32.dll' failed to load (-904) (SQLDriverConnect); [08004] [ODBC Firebird Driver]Invalid connection string attribute (0)")
I am not sure why he tries to find 'gds32.dll'. In the ODBC-Connection I used this driver C:\Program Files (x86)\assfinet ams.5\BIN\FB30\x64\fbclient.dll
I am using Firebird as a 64-bit version, so I am a bit clueless because of the 32 in 'gds32.dll'.
I am not sure, if it is the right way to try it with pyodbc. I am open for other advice.
Has anyone an idea why it is not working?
The fact the error mentions gds32.dll means it tried to load fbclient.dll, and that didn't work. Then it tried to fallback to gds32.dll. The gds32.dll is supported historically, because Firebird was forked from InterBase 22 years ago, and InterBase used the name gds32.dll for its client library. The 64-bit version is also called gds32.dll.
The problem is that, unless the C:\Program Files (x86)\assfinet ams.5\BIN\FB30\x64\ folder is explicitly on the path, or you configured the CLIENT connection property, that no library is found (or possibly it's 32-bit not 64-bit).
You need a 64-bit fbclient.dll. If that C:\Program Files (x86)\assfinet ams.5\BIN\FB30\x64\ is really a 64-bit Firebird (then C:\Program Files (x86) is the wrong location), you either need to specify the path of the 64-bit client library in the CLIENT connection property, or you can install it with - from a command prompt started as administrator - instclient i f from a Windows 64-bit Firebird installation, or do a client install using a Firebird installer. Alternatively, you can download the zipkit of a Windows 64-bit Firebird and use its fbclient.dll.
You should also consider using one of the Firebird drivers for Python, instead of using ODBC. You can choose from:
firebird-driver - uses fbclient.dll
FDB - uses fbclient.dll (deprecated and replaced by firebird-driver)
firebirdsql (aka pyfirebirdsql) - a pure Python driver (no native dependencies)
Also, I'm not sure if Gordon's advice about using SQLAlchemy is correct, but I'd recommend investigating that (though below the covers SQLAlchemy will probably use FDB or maybe firebird-driver, so you'd still need a proper 64-bit client library to load).
If you are going to use pandas with a database other than SQLite you should be using SQLAlchemy (ref: here). In your case you would use the sqlalchemy-firebird dialect.
Edit re: comment to original answer
Since we are connecting to localhost we can expect that Firebird has been installed and therefore the client tools are available (which is true for a default install). In that case, the following works on a Windows 8.1 test machine:
import pandas as pd
import sqlalchemy as sa
# note the r"" string
engine = sa.create_engine(r"firebird://SYSDBA:masterkey#localhost/C:\ProgramData\assfinet\assfinet ams.5\Individuell 2022\DB0 - Stand 2022_02-10.FDB")
df = pd.read_sql_query("SELECT * FROM my_table", engine)
although a better approach would be to build the connection URL like this
connection_url = sa.engine.URL.create(
"firebird",
username="SYSDBA",
password="masterkey",
host="localhost",
database=r"C:\ProgramData\assfinet\assfinet ams.5\Individuell 2022\DB0 - Stand 2022_02-10.FDB",
)
engine = sa.create_engine(connection_url)

Sharing .exe with people who do not have ODBC Driver installed?

I've created an Executable that utilizes pyodbc to connect to SQL Server. I need to share this executable with colleagues who have not downloaded the ODBC driver on their computers which causes the executable to fail to open.
I understand if they were to download the ODBC driver I could adjust the executable to detect the driver of the computer each time to avoid this issue. However, not everyone I share this executable with is technical so I would like to avoid having them install the ODBC driver.
Is there a way to create an executable that connects to SQL Server that does not require the user to have an ODBC driver installed on their computers?
try:
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
except:
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
Windows includes an ODBC driver named "SQL Server" as part of its standard installation so every Windows machine has it. It is targeted to older versions of SQL Server so it doesn't support some of the newer features in the latest releases, but if your needs are fairly basic it might suffice.

Issues connecting to a DB2 server using Python. Possible issue identifying the driver

I had written code to connect to a DB2 server using Python that was working fine until my company had me upgrade the IBM DB2 Driver to 11.5. Now, its no longer working and I'm getting the following error message:
SQLCODE=-30082n: [IBM][CLI Driver] SQL30082N Security processing failed with reason "15" ("PROCESSING FAILURE"). SQLSTATE=08001
I verified that I have the right password. Also, I confirmed that I still have access to the Database by going through the ODBC Manager in Windows and testing a connection that way. I've checked several pages here on Stackoverflow and other sites and nothing seems to work. Most of the issues people are having on this site involve different error messages.
Here is the original code using the ibm_db module:
import ibm_db
import ibm_db_dbi
con = ibm_db_dbi.Connection(ibm_db.connect("ABC1011","rsmith","Passw123"))
When that stopped working i tried connecting using pyodbc as a connection:
import pyodbc
cnxn = pyodbc.connect('DRIVER={IBM DB2 ODBC DRIVER - DB2COPY2};Database=ABC1011;UID=rsmith; PWD=Passw123')
Again, I already verified in the ODBC Datasource Administrator (64-bit) that I am using the correct password, I have the correct database, and that I am using the correct driver's name. Does anyone know what else I may need to include to get a connection to work?
Thank you.

How can i connect Remote MS access using pyodbc

How can i connect to remote MS Access DB using pyodbc, before i can able to connect to MS SQL server using pyodbc via freetds. But if i use the same freetds configuration for remote access, 'i'm not getiing the success.
Any suggestions?
freeTDS is ODBC driver for using the TDS communication protocol. MS SQL Server uses the TDS protocol.
Access does not, so it is not a surprise that it doesn't work. What you need is an ODBC driver for the Jet engine used by Access. And it exists. It is called libmdb. You can install the packages to get it set up.
Read MDB Tools Installation Guide for more information.

SQLAlchemy connects to SQLServer using Windows Authenticaton with pymssql driver

I can use pymssql to connect to SQLServer using Windows Authentication:
conn = pymssql.connect(host='..', database='..', trusted=True)
But how could I use SQLAlchemy to connect to SQLServer using Windows Authenticaton with pymssql driver?
The example given by SQLAlchemy is:
mssql+pymssql://<username>:<password>#<freetds_name>
Since I use Windows Authentication, I cannot manually set the username and password.
I do not have pymssql to check, but try the version below:
mssql+pymssql://<freetds_name>?trusted=True

Categories