How to use variable in SQL query using Python
I'm trying to pull data from my Google Ads account using their API. I'm using Python
In the code example there is a query where I can specify the date range from which I want to collect results from my campaign.
I would like to dynamically change de date everyday and run the script to get data from the previous day.
My idea was to create a variable with the previous day date in the format Google API is requiering it, named "yesterday".
But I don't know how to use this variable into the query
Here's a part of the code :
import argparse import sys from google.ads.googleads.client import GoogleAdsClient from google.ads.googleads.errors import GoogleAdsException from datetime import * yesterday_date = date.today() - timedelta(days=1) yesterday = yesterday_date.strftime("%Y-%m-%d") def main(client, customer_id): ga_service = client.get_service("GoogleAdsService") query = """ SELECT campaign.id, campaign.name, metrics.cost_micros, campaign.status FROM campaign WHERE segments.date = '2021-08-19' """ # Issues a search request using streaming. response = ga_service.search_stream(customer_id=customer_id, query=query) for batch in response: for row in batch.results: print( f"Campaign with ID {row.campaign.id}, status is {row.campaign.status}, cost is {row.metrics.cost_micros} and name " f'"{row.campaign.name}" was found.' )
I would like to do something like this :
WHERE segments.date = yesterday
I've tried to use sqlite3 library, to use cursor.execute() but I don't know how to use them properly.
I'm a beginner so the answer might be very easy.