2014年12月5日金曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers)(Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 17(Databases)、17.10(Exercises) 3.を解いてみる。

17.10(Exercises) 3.

コード(BBEdit)

sample3.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sqlite3

connection = sqlite3.connect('numbers.db')
cur = connection.cursor()

def p():
    for row in cur.fetchall():
        print(row)
        
cur.execute('CREATE TABLE Numbers(Val INTEGER)')
cur.execute('INSERT INTO Numbers Values(1)')
cur.execute('INSERT INTO Numbers Values(2)')
connection.commit()

cur.execute('SELECT * FROM Numbers WHERE 1/0')
p()
cur.execute('SELECT * FROM Numbers WHERE 1/0 AND Val > 0')
p()
cur.execute('SELECT * FROM Numbers WHERE Val > 0 AND 1/0')
p()

cur.execute('SELECT * FROM Numbers')
p()

try:
    1 / 0
except Exception as err:
    print(type(err), err, err.args)

connection.close()

入出力結果(Terminal, IPython)

$ ./sample3.py
(1,)
(2,)
<class 'ZeroDivisionError'> division by zero ('division by zero',)
$

0 コメント:

コメントを投稿