简体   繁体   中英

lambda expression for fetching data from mssql in python

I have mssql table and i want to fetch few columns for some specific id's from that table using lambda function. Id is iterating over for loop.

Can you guide me to get the specific outcome so that I can fetch data of 1 id and append it to empty dataframe?

df=pd.DataFrame()

 for i in range(len(temp)): 
    query="""select Code, Date,Status,Category,Class,Quantity FROM table_name where Code= '""" + str(temp.loc[i,'Code'])+"'"
    new=pd.read_sql(query,conn)
    df = df.append(temp)   
    return df

@pratham, see the logic you need. You can use the tuple function that allows you to pass the list to the query text. The result of the query can be used to build a dataframe. Let me know if it works.

inilist =[4, 34, 6, 9, 0, 5]
t = tuple(inilist)
query = "select Code, Date,Status,Category,Class,Quantity FROM table_name where Code IN {}".format(t)
query

Results:

'select Code, Date,Status,Category,Class,Quantity FROM table_name where Code IN (4, 34, 6, 9, 0, 5)'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM