Hello again!
I am working on a Python application and have a query interacting with an Access 2016 backend that I can't seem to get right. For reference, here is the function in its entirety:
The query portion:
What I am trying to accomplish is if
Any chance one of you experts knows what I am doing wrong? I have never done a "dynamic" query like this before.
Edit:
I can seem to only ever get the query to return all records, filtered records or no records but not a combination of them.
I am working on a Python application and have a query interacting with an Access 2016 backend that I can't seem to get right. For reference, here is the function in its entirety:
Python:
def query_order_data(self, pm_id):
if not self.connection_manager.conn:
self.connection_manager.connect()
sql = '''SELECT tblCustomer.CustomerName, tblJob.JobID, tblJob.JobName, tblJob.IsActive, tblJob.ID_PM, tblOrders.OrderNumber, tblOrders.Status, tblOrders.LastDataFetch
FROM (tblCustomer INNER JOIN tblJob ON tblCustomer.CustomerID = tblJob.ID_Customer) LEFT JOIN tblOrders on tblJob.JobID = tblOrders.ID_Job
WHERE tblJob.IsActive = True
AND (tblJob.ID_PM = ? or ? IS NULL)
'''
try:
cursor = self.connection_manager.conn.cursor()
cursor.execute(sql, (pm_id, pm_id))
returned_orders = cursor.fetchall()
order_list = [Orders(*order) for order in returned_orders] # Convert tuple to list of objects
return order_list
except Exception as e:
print(e)
The query portion:
Python:
sql = '''SELECT tblCustomer.CustomerName, tblJob.JobID, tblJob.JobName, tblJob.IsActive, tblJob.ID_PM, tblOrders.OrderNumber, tblOrders.Status, tblOrders.LastDataFetch
FROM (tblCustomer INNER JOIN tblJob ON tblCustomer.CustomerID = tblJob.ID_Customer) LEFT JOIN tblOrders on tblJob.JobID = tblOrders.ID_Job
WHERE tblJob.IsActive = True
AND (tblJob.ID_PM = ? or ? IS NULL)
'''
What I am trying to accomplish is if
pm_id
is None/Null, return ALL records. However if pm_id
has a value, return records where ID_PM
match pm_id
Any chance one of you experts knows what I am doing wrong? I have never done a "dynamic" query like this before.
Edit:
I can seem to only ever get the query to return all records, filtered records or no records but not a combination of them.
Last edited: