2014年7月19日土曜日

開発環境

Head First SQL ―頭とからだで覚えるSQLの基本 (Lynn Beighley(著)、 佐藤 直生 (監訳)、 松永 多苗子 (翻訳)、オライリージャパン)の2章(SELECT 文: 天賦のデータ検索)、エクササイズ(p.69)を解いてみる。

エクササイズ(p.69)

コード(BBEdit, Emacs)

sample69.py

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

import sqlite3

connection = sqlite3.connect('gregs_list.sqlite')
cursor = connection.cursor()

# 1 SQLiteはMySQLと違って、バックスラッシュによるエスケーウは使えないみたい
try:
    cursor.execute("""
SELECT * FROM my_contacts
WHERE
location = 'ニュージャージー州グローバーズミル (Grover\'s Mill)'
""")
    print(cursor.fetchall())
except Exception as err:
    print(type(err), err, 'バックスラッシュ')

# 2
try:
    cursor.execute("""
SELECT * FROM my_contacts
WHERE
location = 'ニュージャージー州グローバーズミル (Grover''s Mill)'
""")
    print(cursor.fetchall())
except Exception as err:
    print(type(err), err, 'シングルクォート')

connection.close()

入出力結果(Terminal, IPython)

$ ./sample69.py 
<class 'sqlite3.OperationalError'> near "s": syntax error バックスラッシュ
[('スティーブ', 'ファンヨン', 'steve@onionflavoredrings.com', 'M', '1970-04-01', 'パンクミュージシャン', "ニュージャージー州グローバーズミル (Grover's Mill)", '独身', '国家の破壊', '同国人、ギタープレイヤー'), ('スティーブ', 'ファンヨン', 'steve@onionflavoredrings.com', 'M', '1970-04-01', 'パンクミュージシャン', "ニュージャージー州グローバーズミル (Grover's Mill)", '独身', '国家の破壊', '同国人、ギタープレイヤー')]
$

0 コメント:

コメントを投稿