Hi,
I don’t have solid understanding about context manager, objects in memory etc… so sorry for bothering you if this question doesn’t make sense at all…
Assuming code as follows:
import sqlite3
def get_price(item_code):
with sqlite3.connect("/items.db") as conn:
result = conn.execute(
"SELECT price WHERE item_code=:item_code",
dict(item_code=item_code)
)
return result.fetchone()
When I call above function such as my_price = get_price(42)
the Connection
(file resource?) still exists? I mean… that I wrote resource inefficient code?
I read the docs and docs say following
Note The context manager neither implicitly opens a new transaction nor closes the connection. If you need a closing context manager, consider using
contextlib.closing()
.
So, should I have coded like that for manging sqlite database file connection resources efficiently?
(To be honest I used the word efficient and resource without what these word actually means…)
import sqlite3
from contextlib import closing
def get_price(item_code):
with closing(sqlite3.connect("/items.db")) as conn:
result = conn.execute(
"SELECT price WHERE item_code=:item_code",
dict(item_code=item_code)
)
return result.fetchone()
I don’t have a degree about computer science and below-average IQ meaning that I have to put efforts to get understanding about general concepts of what opening the file, efficient resource management(what resource itself), etc means.
Besides just code differences, to get understanding of what happens actually, would you recommend the books, references?, or any youtube?
Thanks a lot for reading this question.
Happy Christmas!