Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95589

Is this the way to go with SQLite

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Is this the way to go with SQLite
Date 2015-08-23 15:18 +0200
Organization Decebal Computing
Message-ID <871teum9c2.fsf@Equus.decebal.nl> (permalink)

Show all headers | 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 | NextNext in thread | Find similar | Unroll thread


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