1

Here I have the function that reads the table, takes the student date the rest to the current date and from that subtraction, I want to update the field (Dias) in the table, I want to do this with each of the records in the table. Here is my query that I use in various parts of the program.

def run_query(self, query, parameters = ()):
    with sqlite3.connect(self.db_name) as conn:
        cursor = conn.cursor()
        result = cursor.execute(query, parameters)
        conn.commit()
    return result

My problem now is that when I put the line ---- self.run_query (query, parameters) ---- inside for the database is blocked, it generates an error, but if I leave it outside the for it only updates the last one registry.

def actualizar_estudiantes(self):
    miFecha=StringVar()
    miID1=StringVar()
    Fec_Act=datetime.datetime.today()
    query = 'SELECT * FROM escuela' 
    db_rows = self.run_query(query) 
    for database_escuela in db_rows:
        wdias=0
        miID1.set(database_escuela[0])
        miID = miID1.get()
        miFecha.set(database_escuela[6])
        Fec_Est = miFecha.get()
        Fec_est1 = datetime.datetime.strptime(str(Fec_Est), '%d/%m/%Y')
        wdias=((Fec_Act - Fec_est1).days)
        query = 'UPDATE escuela SET DIAS1 = ? WHERE ID = ?'
        parameters = (wdias, miID)
        self.run_query(query, parameters)
    messagebox.showinfo("A T E N C I O N ! ! !", "La lista de estudiantes se Actualizó Exitosamente")

If I put the line ---- self.run_query (query, parameters) ---- inside, ERROR DATABASE LOCKED, if I leave it outside, I only update the last line, can you help me or explain how the line should be updated and not the database block me? Please, I have been days with this problem. Thank you

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Juan Carlos Pantoja\AppData\Local\Programs\Python\Python38- 
 32\lib\tkinter\__init__.py", line 1883, in __call__
     return self.func(*args)
   File "index5.py", line 206, in actualizar_estudiantes
     self.run_query(query, parameters)
   File "index5.py", line 106, in run_query
     conn.commit()
 sqlite3.OperationalError: database is locked
  • 1
    Does this answer your question? [How to know which process is responsible for a "OperationalError: database is locked"?](https://stackoverflow.com/questions/53270520/how-to-know-which-process-is-responsible-for-a-operationalerror-database-is-lo) – stovfl May 02 '20 at 16:59
  • No, because my problem is different, I want to update all the records of a table using a for (iterating), it does it right the first time but when it does it the second time, the database is blocked, only I don't know how place the query instruction to record inside the for. Maybe it is a very simple error to fix, but I am starting with Python and I don't know how to do it, I appreciate your help. – Juan carlos Pantoja May 02 '20 at 20:43
  • can someone help me with please – Juan carlos Pantoja May 06 '20 at 22:54
  • 1
    Try changing `return result` to `return [rec for rec in result]` – stovfl May 07 '20 at 08:28
  • Thank you very much, it worked perfectly, I had 3 weeks with this error and did not know how to solve it. Very sincerely grateful. Could you explain the instruction that although it works well I do not understand it, Thankful. – Juan carlos Pantoja May 07 '20 at 14:09
  • 1
    You do a second `.execute(...` while the current `.execute(...` has not finished. A `.execute(` returns a **generator** and has finished if all resulting records are exhausted. My solution appends all records into a anonymous `list` which get returned. Please, replace the `tkinter` tag with `sqlite3`. – stovfl May 07 '20 at 17:28
  • Thank you very much for the explanation, I understood the instruction – Juan carlos Pantoja May 08 '20 at 13:29

0 Answers0