when I first came into contact with the database, I encountered some problems during my practice. After consulting the materials, I still couldn"t find the answer. Several questions are as follows:
question 1: how to place the connection for sqlite3?
Before, I always wrote a function, such as db_connect
, which returned a connection of sqlite3
. After consulting the information on the Internet, I said that I could just take conn
as a global variable, and then if multiple modules refer to conn
, I need to consider ORM
. The link is the comment below the question, which I copied as follows:
yes, for simple code, just open the connection at the top of the script and use it without creating a function. If you have several modules and you find that you want the same connection to be used in all of them (DRY principal), that"s the time to start seriously thinking about an ORM
my problem is that I don"t need to use ORM
, so how do I write it? For example, I have two model
, Student
and Teacher
, all of which have additions, deletions, modifications and queries to the database, then I think of these two ways of writing:
the first direct import
connection
in this , stackoverflow also says that if it is not closed, it will be garbage collected, and there is no more detailed explanation, so it is not necessary to close cursor
? .
the question is a little confused. Please forgive me. If you don"t understand, please point it out in time. If you can answer it, I would appreciate it.