title: How do I lock an entire SQLite connection (locked read + locked write)?
body: <p>I have an sqlite3 db that is being accessed concurrently. I have <code>ClientA</code> that reads the state of some table (Column1 has rows <strong>A</strong>, <strong>B</strong>, <strong>C</strong>) and needs to update the table with new letters of the alphabet. If <code>ClientB</code> reads the state of the table before <code>ClientA</code> updates the table (say with the new letter <strong>D</strong>), then it's possible that both clients could (and in my case <em>do</em>) write <strong>D</strong> to the table - such that Column1 becomes <strong>A</strong>, <strong>B</strong>, <strong>C</strong>, <strong>D</strong>, <strong>D</strong>. But I need to ensure Column1 only has unique letters!</p>
  
  <p>How do I lock the db connection so that its read AND write operations get exclusive access so that Column1 doesn't accidentally change states between some other read-write cycle?</p>
  
  <p>It's hard to find anything about "locking a sqlite read" online because everyone seems more interested in unlocking the db. The following doesn't seem to give <code>con</code>'s read operations <a href="https://docs.python.org/3.6/library/sqlite3.html#sqlite3.Connection.isolation_level" rel="nofollow noreferrer">exclusive access</a></p>
  
  <pre><code>con = sqlite3.connect(db, isolation_level='EXCLUSIVE', timeout=10)
  </code></pre>
  
  <p>Related: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/7914102/file-locking-read-then-write-whilst-locked">File locking - read then write whilst locked</a></li>
  <li><a href="https://stackoverflow.com/questions/5367118/how-perform-sqlite-query-with-a-data-reader-without-locking-database?rq=1">How perform SQLite query with a data reader without locking database?</a></li>
  </ul>