-
9bf7a81f
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
help.md 1.38 KiB
SQLite3
fetchone() method:
The fetchone() method returns the first row of the results as a tuple (or None if no row is available), which is useful when you are expecting at most one row to match your query. It's a way of saying, "Give me the next row of results, if there is one."
the execute() method returns a cursor object, on which you can immediately call fetchone()
the execute() function only sends the SQL command to the database; it doesn't actually retrieve the rows.
commit()
conn.commit() is used to save the changes you make in your database transaction. When you INSERT, UPDATE, or DELETE data in the database, these changes are not immediately saved; they are part of a transaction. By calling conn.commit(), you are telling the database to save all the changes that were made within the transaction. If you don't commit, none of the changes you made during the transaction will be saved to the database.
db.close()
It's a good practice to close your database connection as soon as you're done with it to free up resources.
Managing database connections
It's better to manage your database connections on a per-request basis. This ensures that each request has its own connection, which is opened when the request starts and closed when it finishes. In Flask, this is often done with before_request and teardown_request decorators or in the route itself.