lambda expression for fetching data from mssql in python












0















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









share|improve this question

























  • It is not clear what you are asking. The output of the query can be easily converted to dataframe. why loop? if you pass the list to the query you get your result, then convert to a dataframe.

    – MEdwin
    Nov 13 '18 at 9:59













  • i have around 100 ids..in sql query i have mentioned where clause there i have to iterate over for loop .

    – Pratham
    Nov 13 '18 at 10:09











  • just do a query where id = [those 100 ids]. The result can be converted to a dataframe easily.

    – MEdwin
    Nov 13 '18 at 10:11











  • The trick is to pass those 100 ids into the where clause. No need to loop. The where clause will be modified to be table_name where Code in (id1, id2, id3...)

    – MEdwin
    Nov 13 '18 at 10:11













  • Be aware that Date is a reserved word in MS SQL Server (and Code is in some other variants). I would rename some of those fields or surround them with square brackets.

    – Andy G
    Nov 13 '18 at 10:24
















0















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









share|improve this question

























  • It is not clear what you are asking. The output of the query can be easily converted to dataframe. why loop? if you pass the list to the query you get your result, then convert to a dataframe.

    – MEdwin
    Nov 13 '18 at 9:59













  • i have around 100 ids..in sql query i have mentioned where clause there i have to iterate over for loop .

    – Pratham
    Nov 13 '18 at 10:09











  • just do a query where id = [those 100 ids]. The result can be converted to a dataframe easily.

    – MEdwin
    Nov 13 '18 at 10:11











  • The trick is to pass those 100 ids into the where clause. No need to loop. The where clause will be modified to be table_name where Code in (id1, id2, id3...)

    – MEdwin
    Nov 13 '18 at 10:11













  • Be aware that Date is a reserved word in MS SQL Server (and Code is in some other variants). I would rename some of those fields or surround them with square brackets.

    – Andy G
    Nov 13 '18 at 10:24














0












0








0








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









share|improve this question
















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






python for-loop lambda






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:21









Triyugi Narayan Mani

1,02141527




1,02141527










asked Nov 13 '18 at 9:12









PrathamPratham

94




94













  • It is not clear what you are asking. The output of the query can be easily converted to dataframe. why loop? if you pass the list to the query you get your result, then convert to a dataframe.

    – MEdwin
    Nov 13 '18 at 9:59













  • i have around 100 ids..in sql query i have mentioned where clause there i have to iterate over for loop .

    – Pratham
    Nov 13 '18 at 10:09











  • just do a query where id = [those 100 ids]. The result can be converted to a dataframe easily.

    – MEdwin
    Nov 13 '18 at 10:11











  • The trick is to pass those 100 ids into the where clause. No need to loop. The where clause will be modified to be table_name where Code in (id1, id2, id3...)

    – MEdwin
    Nov 13 '18 at 10:11













  • Be aware that Date is a reserved word in MS SQL Server (and Code is in some other variants). I would rename some of those fields or surround them with square brackets.

    – Andy G
    Nov 13 '18 at 10:24



















  • It is not clear what you are asking. The output of the query can be easily converted to dataframe. why loop? if you pass the list to the query you get your result, then convert to a dataframe.

    – MEdwin
    Nov 13 '18 at 9:59













  • i have around 100 ids..in sql query i have mentioned where clause there i have to iterate over for loop .

    – Pratham
    Nov 13 '18 at 10:09











  • just do a query where id = [those 100 ids]. The result can be converted to a dataframe easily.

    – MEdwin
    Nov 13 '18 at 10:11











  • The trick is to pass those 100 ids into the where clause. No need to loop. The where clause will be modified to be table_name where Code in (id1, id2, id3...)

    – MEdwin
    Nov 13 '18 at 10:11













  • Be aware that Date is a reserved word in MS SQL Server (and Code is in some other variants). I would rename some of those fields or surround them with square brackets.

    – Andy G
    Nov 13 '18 at 10:24

















It is not clear what you are asking. The output of the query can be easily converted to dataframe. why loop? if you pass the list to the query you get your result, then convert to a dataframe.

– MEdwin
Nov 13 '18 at 9:59







It is not clear what you are asking. The output of the query can be easily converted to dataframe. why loop? if you pass the list to the query you get your result, then convert to a dataframe.

– MEdwin
Nov 13 '18 at 9:59















i have around 100 ids..in sql query i have mentioned where clause there i have to iterate over for loop .

– Pratham
Nov 13 '18 at 10:09





i have around 100 ids..in sql query i have mentioned where clause there i have to iterate over for loop .

– Pratham
Nov 13 '18 at 10:09













just do a query where id = [those 100 ids]. The result can be converted to a dataframe easily.

– MEdwin
Nov 13 '18 at 10:11





just do a query where id = [those 100 ids]. The result can be converted to a dataframe easily.

– MEdwin
Nov 13 '18 at 10:11













The trick is to pass those 100 ids into the where clause. No need to loop. The where clause will be modified to be table_name where Code in (id1, id2, id3...)

– MEdwin
Nov 13 '18 at 10:11







The trick is to pass those 100 ids into the where clause. No need to loop. The where clause will be modified to be table_name where Code in (id1, id2, id3...)

– MEdwin
Nov 13 '18 at 10:11















Be aware that Date is a reserved word in MS SQL Server (and Code is in some other variants). I would rename some of those fields or surround them with square brackets.

– Andy G
Nov 13 '18 at 10:24





Be aware that Date is a reserved word in MS SQL Server (and Code is in some other variants). I would rename some of those fields or surround them with square brackets.

– Andy G
Nov 13 '18 at 10:24












1 Answer
1






active

oldest

votes


















0














@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)'





share|improve this answer
























  • Thanks ...It's working fine

    – Pratham
    Nov 13 '18 at 10:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277460%2flambda-expression-for-fetching-data-from-mssql-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














@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)'





share|improve this answer
























  • Thanks ...It's working fine

    – Pratham
    Nov 13 '18 at 10:39
















0














@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)'





share|improve this answer
























  • Thanks ...It's working fine

    – Pratham
    Nov 13 '18 at 10:39














0












0








0







@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)'





share|improve this answer













@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)'






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 10:17









MEdwinMEdwin

974114




974114













  • Thanks ...It's working fine

    – Pratham
    Nov 13 '18 at 10:39



















  • Thanks ...It's working fine

    – Pratham
    Nov 13 '18 at 10:39

















Thanks ...It's working fine

– Pratham
Nov 13 '18 at 10:39





Thanks ...It's working fine

– Pratham
Nov 13 '18 at 10:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277460%2flambda-expression-for-fetching-data-from-mssql-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ