mardi 16 juin 2015

Python SQLite3 global database connection

Can anyone tell me if this will work. When you open a SQLite database connection to your database file in the main of your python application and then call that variable as a global connection each time you need to execute to database in a function.

As seen below. Will this even work?

import sqlite3 as lite
con = lite.connect(database)

def db_add_records(_number, _data):
   global con
   cur = con.cursor()
   cur.execute(whatever sql you think of)
   con.commit()

Instead of each time creating a new connection to the database inside each function, as I have seen some people do.

Cleaner?

Aucun commentaire:

Enregistrer un commentaire