-1
  1. user save the sql query in the pd , i want execute each the sql query then put into the same pd the dataframe like below
   colunm1 = 'id1' , 'id2 '  
   column2 = 'select sum(total) from table where data_mothrn ='2020-04'' , 
             'select sum(total) from table2 where data_month ='2020-04'' , 
   column3 = 'result from column2'
             'result from column2'              
  1. i want to literate this thing? put the each column2 result into column3 by each row
list_para = []
def cacth_column2_sql():
    a = df_parameter['column2'].iloc[i]
    return a

def result_sql (p):
    df0 = pd.read_sql(p, conn)
    a = df0.iat[0, 0]
    list_para.append(a)


a = cacth_column2_sql() 
result_sql(a)

  1. how can i create new column to save the result by automatic ?

1 Answers1

0

Your question requires more inputs from you. Please update it correctly.

Based on what I understand:

I have created a code that reads the input from user (Parameter-name, parameter-data) and shows it in the output. Each record can be accessed by Parameter-name

import pandas as pd

n_times = int(input("How many input queries you want to add"))

res = []
for i in range(0,n_times):

   paramname = input("Enter the id for the query ")
   paramdata = input("Enter the sql query ")
   #column3 = 'result from column2'
   result = pd.read_sql(paramdata, conn)
   #You can add code to check correct syntax here of input
   df1 = pd.DataFrame(data=[[paramname,paramdata,result]],columns= 
   ["Parameter-Name", "Parameter-Data","Result"])
   df1.index = df1.index + 1

print(df1)

You can access the queries by index like this

df1.iloc[0]
Code run
  • 165
  • 9