0

I am trying to access the database while the writing to the database from a different script.

The write part

def tick_entry(timestamp,ltp):
    conn = sqlite3.connect('bnf_tick.db', detect_types=sqlite3.PARSE_DECLTYPES, timeout=20)

     c = conn.cursor()

    c.execute('INSERT INTO bnftick (timestamp, close) VALUES (?,?)',
          (timestamp,ltp))

    conn.commit()

    c.close()

    conn.close()

I am running a for loop on a pandas dataframe and writing the data using tick_entry form above.

I want to be able to access the database while the loop is running but I keep getting:

sqlite3.OperationalError: database is locked

In real-time(right now I am feeding in old data) the data should be a little more sporadic. I am trying to figure out if I will be able to access the database while it is being written to by the above function.

Am I doing something wrong?

Thanks

3
  • 1
    Only one connection can have an open write transaction at a time (and when it does no read transactions are allowed unless it's a WAL database) So if one gets a locked error, wait a short time for the transaction to be committed and the write lock released, and try again. Commented Jan 27, 2019 at 17:09
  • @Shawn thanks. Am I correct to assume I can keep the connection to the database in both scripts but while the db is being written into I cannot read it? Commented Jan 27, 2019 at 17:54
  • Unless you use WAL journal mode, yeah. sqlite.org/wal.html Commented Jan 27, 2019 at 20:04

1 Answer 1

0

SQLite databases are not able to be modified by more than one process at a time. This is a known design limitation, and is not possible to code around.

Sign up to request clarification or add additional context in comments.

3 Comments

Not true. You can have any number of readers, or a single writer, but not both at once (unless using WAL mode).
@Shawn Thank you for clarifying, I've edited my post to reflect this.
I am only trying to read in the other instance. i.e. one script writes to the SQLite database and the other reads from the DB. Is there a specific way to open in "read only"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.