Is this the way to go with SQLite

Chris Angelico rosuav at gmail.com
Mon Aug 24 07:26:43 EDT 2015


On Mon, Aug 24, 2015 at 9:00 PM, Cecil Westerhof <Cecil at decebal.nl> wrote:
> On Sunday 23 Aug 2015 16:03 CEST, Chris Angelico wrote:
>
>> On Sun, Aug 23, 2015 at 11:18 PM, Cecil Westerhof <Cecil at decebal.nl> wrote:
>>> Also an URL is unique, so I need to check that if it is found, the
>>> values are the same as the ones I wanted to insert.
>>
>> And if they aren't? Currently, all you do is print out a message and
>> continue on; what happens if you get the same URL coming up more
>> than once?
>
> That is all what I want at the moment: to get notified when an URL has
> two different descriptions. It is just a script to do an initial fill
> of the table. When run again I do not insert the URLs that are already
> in the database. But just skipping is not enough, when it has a
> different description I did something wrong and should investigate
> that.

Sounds to me like you mostly want an error, but in some cases, you'll
suppress the error (ie it's exactly the same description and
datestamp).

>> error). The risk normally is of a race condition; you could execute
>> your SELECT query, find no results, and then have someone else
>> insert one just a moment before you do. But with SQLite, you're
>> probably assuming no other writers anyway - an assumption which (I
>> think) you can mandate simply by opening a transaction and holding
>> it through the full update procedure - which would make this safe.
>
> I start with:
>     conn = sqlite3.connect('links.sqlite')
>     c = conn.cursor()
>
> and end with:
>     conn.commit()
>     conn.close()
>
> Taken from:
>     https://docs.python.org/2/library/sqlite3.html
>
> This takes care of the transaction, or not?

Yep, I think so. If not, you should be able to ensure transactional
integrity by simply adding an explicit "BEGIN" query.

ChrisA



More information about the Python-list mailing list