mercredi 29 avril 2015

Loading in a new database, error: sqlite3 Cannot operate on a close Database

So I added an option to my GUI(Tkinter) that allows you to load in a new database (via filedialog), but it gives me this:

sqlite3.ProgrammingError: Cannot operate on a closed database.

Here is my code:

def load_database(self):
    file_path = filedialog.askopenfilename( filetypes = ( ("Database Files", "*.dB"), ("All files", "*.*") ) )
    print (file_path)
    if (file_path == "" or file_path == ()):
        return
    else:
        conn.close()
        self.reconnect_to_database(file_path)

def reconnect_to_database(self, file_path):
    conn = sqlite3.connect(file_path)  
    cursor = conn.cursor()
    cursor.execute("""CREATE TABLE if not exists Cards
              (trackOne text, trackTwo text, trackThree text) 
           """)
    conn.commit()

conn = sqlite3.connect("database.db")  
cursor = conn.cursor()

Just wondering how to close the sqlite3 connection and load + opening a new database.

thanks

Aucun commentaire:

Enregistrer un commentaire