Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof 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> 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 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