English 中文(简体)
我是否需要在这样做之后明确叫 con。 execute execute
原标题:Do I need to explicitely call con.close() after having done con.execute in SQLite?

Suppose we have this code:

def _te(tn: str, db_id: str) -> bool:
    with _connect_db(db_id) as conn:
        cur = conn.cursor()    
        cur.execute("SELECT count(*) FROM sqlite_master WHERE type= table  AND name=?", (tn,))
        
        return cur.fetchone()[0] == 1


def _connect_db(db_id: str) -> sqlite3.Connection:
    return sqlite3.connect(db_id)

是否需要在<代码>_te的末尾打电话?

我认为,我的理解似乎自相矛盾的信息:

On one hand, this accepted answer says (with a link to SQLite3 source code): https://stackoverflow.com/a/25988110/9661990

不要担心关闭数据库。 当你要求准备或执行时,这些电话一旦发出,即自动停止。 有一个内部救助/检查组,确保即使出现错误,也关闭 d。 你们可以在Kalk3来源代码中看到这一点:数据库。

But on the other hand, this SQLite3 doc: https://docs.python.org/3/library/sqlite3.html

has a snippet with execute, and yet in this snippet it specifically commands a manual close:

# Create and fill the table.
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE lang(name, first_appeared)")
data = [
    ("C++", 1985),
    ("Objective-C", 1984),
]
con.executemany("INSERT INTO lang(name, first_appeared) VALUES(?, ?)", data)

# Print the table contents
for row in con.execute("SELECT name, first_appeared FROM lang"):
    print(row)

print("I just deleted", con.execute("DELETE FROM lang").rowcount, "rows")

# close() is not a shortcut method and it s not called automatically;
# the connection object should be closed manually
con.close()
最佳回答

关闭Kall3号连接的仅有两条(*)项是<代码>.close()方法,并处理连接标的的位置(在连接标的偏离范围后将发生一段时间,如果未达到,则肯定会在正常的飞离现场)。 如你所指出,<>连接的<代码>只有在离开时才会承诺或退缩,而不是关闭。

采用os._exit、使SIGKILLed或计算机关闭等异常做法,并不能切断联系。 见。 如果不关闭三氯苯乙胺,数据是否可能丢失?;你还可以见,该说明:sqlite3_close_v2,该代码是<.close()(除了释放物体本身的任何资源外)。

理想的情况是,你的方案将使用有限的联系,通常只是联系。 通常不明确关闭这些联系,因为在方案退出时,这些联系将自动关闭(阻碍上述设想)。 这样做仍然是一种良好做法。

然而,如果你在 lo中打开了联系(尽管我不认为这是一个好的想法),你必须明确关闭这些联系,或冒着失去你的电脑资源的风险。 每一条连接都带有一些记忆,一个记录员;两者都供应有限。


(*) 链接在打上已经开放的连接的<条码>_init__(时也关闭,但这一点并不重要。

问题回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签