I have a list with three elements in it and I want to iterate over the list to execute query one after another element in the list. For example,
zipcode_list = ['10000', '10018', '11201']
sql = "SELECT name, zipcode, state FROM survey WHERE zipcode IN (%s)"
I want to execute query "SELECT name, zipcode, state FROM survey WHERE zipcode IN 1000"
first, and then "SELECT name, zipcode, state FROM survey WHERE zipcode IN 10018"
and last query "SELECT name, zipcode, state FROM survey WHERE zipcode IN 11201"
I also want to put the retrieved data in 3 separate dataframes. Here's how I executed the query,
zipcode_list = ['10000', '10018', '11201']
sql = "SELECT name, zipcode, state FROM survey WHERE zipcode IN (%s)"
in_p = ', '.join(list(map(lambda x: '%s', zipcode_list)))
sql = sql % in_p
df = cursor.execute(sql, zipcode_list).fetchall()
for dfs in df:
df = pd.DataFrame(df)