why should one use functions cursor and connection close in python after connecting to SQL?









up vote
1
down vote

favorite












Im trying to understand the need for a cursor() and connection close() functions within a python script when you are trying to connect to a SQL database.



I currently have the following code and it works fine:



con = pyodbc.connect("Driver=SQL Server Native Client 11.0;"
"Server=servername;"
"Database=table;"
"Trusted_Connection=yes;") # type: object

sql = """select top 1000 * from Bill.dbo.Table"""
df = pd.read_sql_query(sql, con)
print(df)


However, some are using the following which I am not sure why:



cursor = conn.cursor()
conn.close


In addition to this question, is there a more efficient way of writing the code ive inserted up there ? thank you










share|improve this question



























    up vote
    1
    down vote

    favorite












    Im trying to understand the need for a cursor() and connection close() functions within a python script when you are trying to connect to a SQL database.



    I currently have the following code and it works fine:



    con = pyodbc.connect("Driver=SQL Server Native Client 11.0;"
    "Server=servername;"
    "Database=table;"
    "Trusted_Connection=yes;") # type: object

    sql = """select top 1000 * from Bill.dbo.Table"""
    df = pd.read_sql_query(sql, con)
    print(df)


    However, some are using the following which I am not sure why:



    cursor = conn.cursor()
    conn.close


    In addition to this question, is there a more efficient way of writing the code ive inserted up there ? thank you










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Im trying to understand the need for a cursor() and connection close() functions within a python script when you are trying to connect to a SQL database.



      I currently have the following code and it works fine:



      con = pyodbc.connect("Driver=SQL Server Native Client 11.0;"
      "Server=servername;"
      "Database=table;"
      "Trusted_Connection=yes;") # type: object

      sql = """select top 1000 * from Bill.dbo.Table"""
      df = pd.read_sql_query(sql, con)
      print(df)


      However, some are using the following which I am not sure why:



      cursor = conn.cursor()
      conn.close


      In addition to this question, is there a more efficient way of writing the code ive inserted up there ? thank you










      share|improve this question















      Im trying to understand the need for a cursor() and connection close() functions within a python script when you are trying to connect to a SQL database.



      I currently have the following code and it works fine:



      con = pyodbc.connect("Driver=SQL Server Native Client 11.0;"
      "Server=servername;"
      "Database=table;"
      "Trusted_Connection=yes;") # type: object

      sql = """select top 1000 * from Bill.dbo.Table"""
      df = pd.read_sql_query(sql, con)
      print(df)


      However, some are using the following which I am not sure why:



      cursor = conn.cursor()
      conn.close


      In addition to this question, is there a more efficient way of writing the code ive inserted up there ? thank you







      python






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 17:08









      jwodder

      31.8k34877




      31.8k34877










      asked Nov 9 at 16:51









      Ismafoot

      497




      497






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          When you create a connection or a cursor, there are resources that are created and represented by that cursor. If you don't close them, then those resources could stay around longer than necessary and potentially leak. It's good hygiene.



          As for efficiency, python has with blocks that work with context managers to make this easier. At the end of a with block the __exit__ method is called which usually defers to close (though for the pyodbc connection object it doesn't).



          with conn.cursor() as cursor:
          #do things with cursor





          share|improve this answer






















          • With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
            – nimish
            Nov 10 at 3:59










          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',
          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%2f53230079%2fwhy-should-one-use-functions-cursor-and-connection-close-in-python-after-connect%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








          up vote
          3
          down vote



          accepted










          When you create a connection or a cursor, there are resources that are created and represented by that cursor. If you don't close them, then those resources could stay around longer than necessary and potentially leak. It's good hygiene.



          As for efficiency, python has with blocks that work with context managers to make this easier. At the end of a with block the __exit__ method is called which usually defers to close (though for the pyodbc connection object it doesn't).



          with conn.cursor() as cursor:
          #do things with cursor





          share|improve this answer






















          • With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
            – nimish
            Nov 10 at 3:59














          up vote
          3
          down vote



          accepted










          When you create a connection or a cursor, there are resources that are created and represented by that cursor. If you don't close them, then those resources could stay around longer than necessary and potentially leak. It's good hygiene.



          As for efficiency, python has with blocks that work with context managers to make this easier. At the end of a with block the __exit__ method is called which usually defers to close (though for the pyodbc connection object it doesn't).



          with conn.cursor() as cursor:
          #do things with cursor





          share|improve this answer






















          • With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
            – nimish
            Nov 10 at 3:59












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          When you create a connection or a cursor, there are resources that are created and represented by that cursor. If you don't close them, then those resources could stay around longer than necessary and potentially leak. It's good hygiene.



          As for efficiency, python has with blocks that work with context managers to make this easier. At the end of a with block the __exit__ method is called which usually defers to close (though for the pyodbc connection object it doesn't).



          with conn.cursor() as cursor:
          #do things with cursor





          share|improve this answer














          When you create a connection or a cursor, there are resources that are created and represented by that cursor. If you don't close them, then those resources could stay around longer than necessary and potentially leak. It's good hygiene.



          As for efficiency, python has with blocks that work with context managers to make this easier. At the end of a with block the __exit__ method is called which usually defers to close (though for the pyodbc connection object it doesn't).



          with conn.cursor() as cursor:
          #do things with cursor






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 3:58

























          answered Nov 9 at 18:44









          nimish

          2,79721628




          2,79721628











          • With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
            – nimish
            Nov 10 at 3:59
















          • With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
            – nimish
            Nov 10 at 3:59















          With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
          – nimish
          Nov 10 at 3:59




          With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
          – nimish
          Nov 10 at 3:59

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230079%2fwhy-should-one-use-functions-cursor-and-connection-close-in-python-after-connect%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

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20