sqlite3_backend.py 898 Bytes
Newer Older
Antonin Dvorak's avatar
Antonin Dvorak committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sqlite3 

def modify_db(conn,
              query,
              *args):
    """Wrapper for modifications (INSERT, UPDATE, DELETE or REPLACE) of Sqlite3 database.

    Executes given query and commits it - in case of lock it retries the commit

    Params
    ------
    connection: sqlite3 connection
        existing sqlite3 connection to use
    query: str
        query to run
    args: array
        additional arguments for execute query, optional
    """
       
    with conn.cursor() as c:  
        if args:
            c.execute(query,args)
        else:
            c.execute(query)     

    # retry commit - db might be locked by different process
    for x in range(0, 10):
        try:
            conn.commit()
        except:
            print("Commit to sqlite unsuccessful, retrying....")
            time.sleep(1)
            pass
        finally:
            break