DEN_VER
DEN_VER

Reputation: 69

python sqlite3 Getting error: sqlite3.OperationalError: database is locked

Can you help me, I wrote this code:

class Favorits(object):
    def __init__(self, graph_):
        self.graph_ = graph_
        self.conn3 = sqlite3.connect('C:/C/V2.db')

    def add_(self):
        с3 = self.conn3.cursor()
        с3.execute("SELECT COUNT(*) FROM fav")
        t_count = с3.fetchall()
        self.conn3.commit()
        t_count = t_count[0][0]

        to_add_rus = self.graph_.text_rus.get('1.0', 'end')
        to_add_eng = self.graph_.text_eng.get('1.0', 'end')
        to_add_esp = self.graph_.text_esp.get('1.0', 'end')
        с3.execute("INSERT INTO fav VALUES(?,?,?,?)", (t_count + 1,    to_add_rus, to_add_eng, to_add_esp))
        self.conn3.commit()

    def rem_(self):
        c4 = self.conn3.cursor()

        idx = (self.graph_.word_.f_0_to_remove)
        idx = idx[0]
        print(idx)
        c4.execute("DELETE FROM fav WHERE id_=?", (idx,))
        self.conn3.commit()

This is a class which I use to add and remove different rows from db (using Tkinter as GUI). So basically I'm trying to make two different connections to the same db via two different cursors (in order to be able to add and remove words from it). And I constantly get this error: self.conn3.commit() sqlite3.OperationalError: database is locked

I already tried differend options, makin' two different cursors, etc. nothing helps.

Upvotes: 0

Views: 1291

Answers (1)

random.george 11
random.george 11

Reputation: 168

You need to close the database connection after opening it. Otherwise the connection persists, and so the database believes an edit is underway so locks itself.

You can break the connection with the command self.conn3.close().

Upvotes: 0

Related Questions