Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95589
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Cecil Westerhof <Cecil@decebal.nl> |
| Newsgroups | comp.lang.python |
| Subject | Is this the way to go with SQLite |
| Date | Sun, 23 Aug 2015 15:18:53 +0200 |
| Organization | Decebal Computing |
| Lines | 38 |
| Message-ID | <871teum9c2.fsf@Equus.decebal.nl> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="9766"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FDyVBeo5I9Y9XTXQkkHhcFL9aR/RTEmU=" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
| X-Homepage | http://www.decebal.nl/ |
| Cancel-Lock | sha1:iXf71IVD6DwRM2apWWGxN0Z4+cM= sha1:s7hRQWNTNb6mbjicu3lY8MGKx9g= |
| Xref | csiph.com comp.lang.python:95589 |
Show key headers only | View raw
I understood that with sqlite3 in Python you can not use prepared
statements. Below the way I solved this.
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.
This is my code.
========================================================================
select_url = '''SELECT year
, month
, description
FROM LINKS
WHERE URL = ?'''
year = 2015
month = 8
for link in links:
description = link[0]
url = link[1]
url_values = c.execute(select_url, [url]).fetchall()
if len(url_values) == 0:
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))
========================================================================
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.python | Previous | Next — 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