I cannot figure out why this is not working. I'm trying to pass a list of strings as part of a loop inside a string that is used as database query. This is the error I'm getting:
\porfiler03.ar.local\gtdshare\vortex\monthly_variability\scripts\wind_index_chinook_region_3plots_working_monthly.py:71 in get_db_data
crs_obj.execute(query)
UndefinedColumn: column "east" does not exist
LINE 5: ...s_name = 'UNITED STATES' AND maint_region_name = EAST AND ...
Here is the code:
regions = ['EAST','CENTRAL']
for x in regions:
randall_query = f"""select plant_mos_adj_wind.plant_name, plant_mos_adj_wind.business_name,plant_mos_adj_wind.business_code,plant_mos_adj_wind.wind_speed_ms,
plant_mos_adj_wind.maint_region_name, plant_mos_adj_wind.wind_direction_deg, plant_mos_adj_wind.mos_time, plant_mos_adj_wind.total_current_capacity,
plant_mos_adj_wind.dataset
from vortex.plant_mos_adj_wind
where business_name = 'UNITED STATES' AND maint_region_name = {x} AND dataset = 'ERA5'"""
try:
connection = psycopg2.connect(user="cms_app",
password="xxxxx",
host="frupaapg02.ar.local",
port="xxxx",
database="xxxx")
print("Selecting rows from vortex plant_mos_adj_wind table using cursor.fetchmany")
cursor = connection.cursor()
cdata = get_db_data(cursor, randall_query, False, True).round(1) #panda df
postgreSQL_select_Query = f"""select plant_name, business_name, maint_region_name, total_current_capacity, mos_time, wind_speed_ms, wind_direction_deg, dataset
from vortex.plant_mos_adj_wind
where business_name = 'UNITED STATES' AND maint_region_name = {x} AND dataset = 'ERA5'"""
cursor.execute(postgreSQL_select_Query)
chinookmos_records = cursor.fetchall()
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
#iterate the columns
print("Column headers for cdata are:")
for col in cdata.columns:
print(col)
This should work using the f string function and substituting for x in regions but you can see that it fails because of the EAST argument and it clearly should be 'EAST' passes as a string is my thought.