<div class="gmail_quote">On Sun, Nov 21, 2010 at 8:42 PM, Andy Dustman <span dir="ltr">&lt;<a href="mailto:farcepest@gmail.com" target="_blank">farcepest@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">


<div><div></div><div><br>
<br>
</div></div>You never actually set sql anywhere, so you&#39;ll always get a NameError<br>
instead of IOError. It would probably be better to not catch the<br>
exception at all in this function.<br>
<font color="#888888">--<br>
Question the answers<br>
</font><div><div></div><div>_______________________________________________<br></div></div></blockquote><div> <br>Thank you, Andy!<br><br>I missed that. That&#39;s why open source code is a good thing. More eyes looking finds problems.<br>


This time I actually entered the code into a file and executed it.  A working version follows:<br><br>&lt;code&gt;<br>from __future__ import print_function<br>import sqlite3<br>import sys<br>def createdb(db):<br>    try:<br>

        con = sqlite3.connect(db)<br>        cur = con.cursor()<br>        sql = &#39;&#39;&#39;<br>           CREATE TABLE t1<br>           (<br>               kid INTEGER PRIMARY KEY,<br>               c1 TEXT,<br>               c2 TEXT<br>

           )&#39;&#39;&#39;<br>        cur.execute(sql)<br><br>        sql = &#39;&#39;&#39;<br>           CREATE TABLE t2<br>           (<br>               kid INTEGER PRIMARY KEY,<br>               c1 TEXT,<br>               c2 TEXT<br>

           )&#39;&#39;&#39;<br>        cur.execute(sql)<br><br>        sql =  &#39;&#39;&#39;<br>           CREATE TABLE t3<br>           (<br>               kid INTEGER PRIMARY KEY,<br>               c1 TEXT,<br>               c2 TEXT<br>

           )&#39;&#39;&#39;<br>        cur.execute(sql)<br><br>        con.commit()<br>    except sqlite3.Error:<br>        print(&quot;ERROR: createdb did not commit.&quot;, file=sys.stderr)<br>        print(&quot;tried this sql:&quot;, file=sys.stderr)<br>

        print(sql, file=sys.stderr)<br>        raise<br>    finally:<br>        cur.close()<br>        con.close()<br><br>if __name__ == &#39;__main__&#39;:<br>    createdb(&#39;myDbName&#39;)<br>&lt;/code&gt;<br><br>This script will run once without errors.<br>

If you run it a second time, you get:<br><br>&lt;console output&gt;<br>ERROR: createdb did not commit.<br>tried this sql:<br><br>           CREATE TABLE t1<br>           (<br>               kid INTEGER PRIMARY KEY,<br>               c1 TEXT,<br>

               c2 TEXT<br>           )<br>Traceback (most recent call last):<br>  File &quot;x.py&quot;, line 44, in &lt;module&gt;<br>    createdb(&#39;myDbName&#39;)<br>  File &quot;x.py&quot;, line 14, in createdb<br>
    cur.execute(sql)<br>
sqlite3.OperationalError: table t1 already exists<br>&lt;/console output&gt;<br><br>Which is exactly what you should expect.  Note that using the naked &quot;raise&quot; statement results in a higher quality traceback with error text pinpointing the problem. <br>

<br>I used print as a function, so this example should work in python versions from 2.6 to 3.2.<br>--<br>Vernon<br><br></div></div>