Check out Kite (free AI Coding Assistant) → Link



Buy Me a Coffee? Your support is much appreciated!

PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn

Source Code:  

import os
from PyQt5.QtSql import QSqlDatabase, QSqlQuery

"""
Connect to a database system
"""
# Specify the driver type
database = QSqlDatabase.addDatabase('QODBC')

# Establish Database Connection
database.setDatabaseName("""
    DRIVER={{Microsoft Access Driver (*.mdb, *.accdb)}};
    DBQ={0}
""".format(os.path.join(os.getcwd(), 'db.accdb')))

if not database.open():
    print('Database is not connected')

"""
Query records
"""    
query = QSqlQuery()
query.exec("SELECT * FROM Customers WHERE [Job Title] = 'Owner'")

query.first()
query.value(3)
query.value('First Name')

query.last()
query.at()

query.seek(5)

query.next()

database.close()