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


Groups > comp.lang.python > #57858

Re: Try-except for flow control in reading Sqlite

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'schema': 0.05; 'none,': 0.07; 'exist,': 0.09; 'lookup': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; '(0,': 0.16; 'conditional': 0.16; 'exit.': 0.16; 'flow.': 0.16; 'message-id:@4ax.com': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sqlite': 0.16; 'subject:flow': 0.16; 'all,': 0.19; 'file,': 0.19; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'this?': 0.23; 'controlling': 0.24; 'entries': 0.24; 'url:home': 0.24; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'testing': 0.29; 'instruction': 0.29; "doesn't": 0.30; 'database,': 0.30; 'needed.': 0.30; 'returned': 0.30; 'specified': 0.30; 'file': 0.32; 'open': 0.33; 'checking': 0.33; 'table': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'except': 0.35; 'something': 0.35; 'acceptable': 0.36; 'executing': 0.36; 'charset:us-ascii': 0.36; 'experience,': 0.37; 'received:76': 0.38; 'needed': 0.38; 'to:addr :python-list': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'length': 0.61; 'name': 0.63; 'victor': 0.84; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Try-except for flow control in reading Sqlite
Date Mon, 28 Oct 2013 19:43:19 -0400
Organization IISS Elusive Unicorn
References <ac7cf18f-d4f4-41ef-966d-a35d2d0e39a8@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-76-253-110-186.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 <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.1728.1383003802.18130.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383003802 news.xs4all.nl 15977 [2001:888:2000:d::a6]:44084
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:57858

Show key headers only | View raw


On Sun, 27 Oct 2013 20:43:07 -0700 (PDT), Victor Hooi
<victorhooi@gmail.com> declaimed the following:

>Hi,
>
>I'd like to double-check something regarding using try-except for controlling flow.
>
>I have a script that needs to lookup things in a SQLite database.
>
>If the SQLite database file doesn't exist, I'd like to create an empty database, and then setup the schema.
>
>Is it acceptable to use try-except in order to achieve this? E.g.:
>
>    try:
>        # Try to open up the SQLite file, and lookup the required entries
>    except OSError:
>        # Open an empty SQLite file, and create the schema
>
>
	In my experience, SQLite will /create/ an empty database file if the
specified name does not exit. So just executing the connect() call is all
that is needed. After all, checking for data IN the database will either
return something or fail at that point in which case you can now populate
the schema.

-=-=-=-=-=-
>>> import sqlite3 as db
>>> con = db.connect("anUnknown.db")
>>> cur = con.cursor()
>>> rst = cur.execute("pragma table_info('aTable')")
>>> rst
<sqlite3.Cursor object at 0x0000000004725C00>
>>> for ln in rst:
... 	print ln
... 	
>>> for ln in cur:
... 	print ln
... 	
>>> rst = cur.execute("create table aTable ( junk varchar )")
>>> con.commit()
>>> rst = cur.execute("pragma table_info('aTable')")
>>> for ln in rst:
... 	print ln
... 
(0, u'junk', u'varchar', 0, None, 0)
>>> 


	No try/except needed -- just an a conditional testing the length of the
result returned by the pragma instruction on the table you expect to find
in the database.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Try-except for flow control in reading Sqlite Victor Hooi <victorhooi@gmail.com> - 2013-10-27 20:43 -0700
  Re: Try-except for flow control in reading Sqlite Steven D'Aprano <steve@pearwood.info> - 2013-10-28 06:18 +0000
    Re: Try-except for flow control in reading Sqlite Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-10-28 10:48 +0100
  Re: Try-except for flow control in reading Sqlite Chris Angelico <rosuav@gmail.com> - 2013-10-28 17:36 +1100
    Re: Try-except for flow control in reading Sqlite Victor Hooi <victorhooi@gmail.com> - 2013-10-27 23:57 -0700
      Re: Try-except for flow control in reading Sqlite Chris Angelico <rosuav@gmail.com> - 2013-10-28 18:01 +1100
        Re: Try-except for flow control in reading Sqlite Steven D'Aprano <steve@pearwood.info> - 2013-10-28 07:19 +0000
      Re: Try-except for flow control in reading Sqlite Chris Angelico <rosuav@gmail.com> - 2013-10-28 18:02 +1100
  Re: Try-except for flow control in reading Sqlite Burak Arslan <burak.arslan@arskom.com.tr> - 2013-10-28 10:17 +0200
  Re: Try-except for flow control in reading Sqlite Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-28 19:43 -0400
    Re: Try-except for flow control in reading Sqlite Victor Hooi <victorhooi@gmail.com> - 2013-10-31 16:08 -0700
      Re: Try-except for flow control in reading Sqlite Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-31 22:45 -0400

csiph-web