Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newspeer1.nac.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'interfaces': 0.04; 'elif': 0.05; 'continuation': 0.07; 'cursor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'exist;': 0.16; 'exit.': 0.16; 'message-id:@4ax.com': 0.16; 'name):': 0.16; 'none.': 0.16; 'operation,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'shortcut': 0.16; 'subject:sqlite3': 0.16; 'language': 0.16; 'wed,': 0.18; 'skip:p 40': 0.19; 'select': 0.22; 'creating': 0.23; "aren't": 0.24; 'url:home': 0.24; 'somewhere': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'subject:) ': 0.29; 'probably': 0.32; "i'd": 0.34; 'subject: (': 0.35; 'connection': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'object,': 0.36; 'returning': 0.36; 'doing': 0.36; 'charset :us-ascii': 0.36; 'needed': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'anything': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'transaction.': 0.60; 'new': 0.61; 'name': 0.63; 'such': 0.63; 'subject:more': 0.64; 'here': 0.66; 'close': 0.67; 'useful.': 0.68; 'results': 0.69; 'records': 0.73; 'avoids': 0.84; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: How to use SQLite (sqlite3) more efficiently Date: Wed, 04 Jun 2014 22:40:17 -0400 Organization: IISS Elusive Unicorn References: <6e18a509-9471-4fe8-8664-3c58fc17536f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-79-222-51.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401936028 news.xs4all.nl 2846 [2001:888:2000:d::a6]:51253 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72669 On Wed, 4 Jun 2014 13:27:39 -0700 (PDT), ps16thypresenceisfullnessofjoy@gmail.com declaimed the following: >def get_description(conn, name): > cur = conn.cursor() > cur.execute("SELECT description FROM ProgrammingLanguages WHERE Name=?", > (name,)) > row = cur.fetchone() > if row: > return row[0] > return None The last line is not really needed -- falling off the end of function results in returning None. While /maybe/ not required for a SELECT operation, I'd put a conn.commit() somewhere in there before the return(s). The standard for Python DB-API interfaces is that auto-commit is turned off -- meaning the SELECT has started a database transaction. > > >def set_description(conn, name, description): > cur = conn.cursor() > cur.execute("SELECT 1 FROM ProgrammingLanguages WHERE Name=?", (name,)) > row = cur.fetchone() > if description: > with conn: This isn't really doing anything useful. You aren't opening a new connection object, so there isn't really anything to close on block exit. > if not row: > conn.execute("INSERT INTO ProgrammingLanguages VALUES(?,?)", > (name, description)) And here you are using a "convenience" shortcut (execute from connection rather than creating a cursor -- which you already have!). > else: > conn.execute("UPDATE ProgrammingLanguages SET Description=? " \ > "WHERE Name=?", (description, name)) > elif row: > with conn: > conn.execute("DELETE FROM ProgrammingLanguages WHERE Name=?", > (name,)) > conn.commit() Untested, but I'd probably end up with something like... def set_description(conn, name, description=None): cur = conn.cursor() #only interested in knowing if any such exist; count() ensures a value cur.execute("select count(*) from ProgrammingLanguages where Name = ?", (name,)) if cur.fetchone()[0] == 0: if description: cur.execute("insert into ProgrammingLanguages values (?, ?)", (name, description)) else: #WARNING -- request to delete non-existant language records else: if description: #note use of triple quotes; avoids the line continuation \ need cur.execute("""update ProgrammingLanguages set Description = ? where Name = ?""", (description, name)) else: cur.execute("delete from ProgrammingLanguages where Name = ?", (name,)) conn.commit() cur.close() -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/