Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95590
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'column': 0.07; 'cc:addr:python-list': 0.09; 'literal': 0.09; 'throw': 0.09; 'anyway': 0.11; '23,': 0.16; 'description)': 0.16; 'error).': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inclined': 0.16; 'query,': 0.16; 'reasonable.': 0.16; 'url)': 0.16; '{0}': 0.16; 'wrote:': 0.16; 'string': 0.17; 'duplicate': 0.18; 'found,': 0.18; 'merge': 0.18; '(in': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'otherwise,': 0.20; 'do.': 0.22; 'assuming': 0.22; 'month,': 0.22; 'select': 0.23; 'insert': 0.23; 'header:In-Reply-To:1': 0.24; '(which': 0.26; 'error': 0.27; 'question': 0.27; 'message- id:@mail.gmail.com': 0.27; 'pieces': 0.27; 'values': 0.28; 'looks': 0.29; 'currently,': 0.29; 'description,': 0.29; 'pep': 0.29; 'unique,': 0.29; "i'm": 0.30; 'print': 0.30; 'transaction': 0.30; 'code': 0.30; 'branch': 0.30; 'normally': 0.30; 'query': 0.30; 'skip:[ 10': 0.31; "i'd": 0.31; 'probably': 0.31; 'received:google.com': 0.35; 'ones': 0.35; 'could': 0.35; "isn't": 0.35; 'but': 0.36; 'instead': 0.36; 'skip:{ 10': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'there,': 0.37; 'say': 0.37; 'wanted': 0.37; 'someone': 0.38; 'subject:the': 0.39; 'where': 0.40; 'subject:with': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'more': 0.63; 'different': 0.63; 'risk': 0.68; 'race': 0.72; 'cecil': 0.84; 'chrisa': 0.84; 'links:': 0.84; 'westerhof': 0.84; 'subject:this': 0.85; 'to:none': 0.91; 'results,': 0.91; 'safe.': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=fEf3ZjdLstZLW1LQXGRGGKfBd54lTt/hNcuU+T/sAvU=; b=BtllElwf1F8Dp5eXoZsoXeabZrHIMUBirvYqe5FiYvKEhLDTH7Vrg0ITRzsR2eXSmQ /yU73WopZloEDauSWG9SNqlnQhtgcRkm1oyZaRSigTgYa4EYEx1OMteWDL+zuKCEcSrN udbF27Qb0z2nq/hcAdFzB2gnfuTFkLr1wru7wkZ5fh8/03sGqUrAK6TU1q685G52jIWV lB0+Yo4nx1erQNjZEymqfhsUTQBCr5UbZILnyl3wLuWOifHaat/x2r0fv7UzbRK8cWHA gdvmqseeGv2AqphYiLDOe4NLsQbliafPEeUlxgs+ESoW/CunPfzuL1/3UOYaMo7c38jk J4/g== |
| MIME-Version | 1.0 |
| X-Received | by 10.50.21.10 with SMTP id r10mr1018163ige.94.1440338633914; Sun, 23 Aug 2015 07:03:53 -0700 (PDT) |
| In-Reply-To | <871teum9c2.fsf@Equus.decebal.nl> |
| References | <871teum9c2.fsf@Equus.decebal.nl> |
| Date | Mon, 24 Aug 2015 00:03:53 +1000 |
| Subject | Re: Is this the way to go with SQLite |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.34.1440338643.17298.python-list@python.org> (permalink) |
| Lines | 60 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1440338643 news.xs4all.nl 23780 [2001:888:2000:d::a6]:47154 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:95590 |
Show key headers only | View raw
On Sun, Aug 23, 2015 at 11:18 PM, Cecil Westerhof <Cecil@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?
> select_url = '''SELECT year
> , month
> , description
> FROM LINKS
> WHERE URL = ?'''
> year = 2015
> month = 8
PEP 8 has a word or two to say about this, but carry on. Incidentally,
I'd be inclined to put the SELECT query down below, same as the INSERT
query is; it's not in any way different from just using a string
literal there, and this separates two pieces of code (IMO)
unnecessarily.
> for link in links:
> description = link[0]
> url = link[1]
for description, url in links:
> url_values = c.execute(select_url, [url]).fetchall()
> if len(url_values) == 0:
if not url_values:
> print('Adding {0}'.format(link))
> c.execute('''INSERT INTO links
> (year, month, description, URL)
> VALUES
> (?, ?, ?, ?)
> ''',
> [year, month, description, url])
> else:
> to_insert = (year, month, description)
> found = url_values[0]
> if found != to_insert:
> print('For {0} found {1} instead of {2}'.format(url, found, to_insert))
Otherwise, looks reasonable. I'm normally expecting to see this kind
of "query, and if it isn't there, insert" code to have an UPDATE in
its other branch (which makes it into a classic upsert or merge
operation - what MySQL calls "INSERT... ON DUPLICATE KEY UPDATE"), or
else throw an error (in which case the cleanest way is to put a unique
key on the column in question and let the database throw the 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.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is this the way to go with SQLite Cecil Westerhof <Cecil@decebal.nl> - 2015-08-23 15:18 +0200
Re: Is this the way to go with SQLite Chris Angelico <rosuav@gmail.com> - 2015-08-24 00:03 +1000
Re: Is this the way to go with SQLite Cecil Westerhof <Cecil@decebal.nl> - 2015-08-24 13:00 +0200
Re: Is this the way to go with SQLite Chris Angelico <rosuav@gmail.com> - 2015-08-24 21:26 +1000
Re: Is this the way to go with SQLite Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-08-23 12:17 -0400
Re: Is this the way to go with SQLite Chris Angelico <rosuav@gmail.com> - 2015-08-24 02:22 +1000
csiph-web