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
python
add a comment |
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
python
add a comment |
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
python
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
python
edited Nov 9 at 17:08
jwodder
31.8k34877
31.8k34877
asked Nov 9 at 16:51
Ismafoot
497
497
add a comment |
add a comment |
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
With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
– nimish
Nov 10 at 3:59
add a comment |
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
With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
– nimish
Nov 10 at 3:59
add a comment |
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
With statements aren't context managers per se, see: docs.python.org/3/reference/datamodel.html#context-managers
– nimish
Nov 10 at 3:59
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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