計算機科学のブログ

ほしい物リスト

SQL - SQLite - SELECT - 天賦のデータ検索 - シングルクォート, エスケープ, ダブルクォート

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

コード

sample4.py

#! /usr/bin/env python3

import sqlite3

con = sqlite3.connect('gregs_list.db')
cur = con.cursor()

sqls = [
    '''
select * from my_contacts
where
location = "Grover's Mill"
''',
    '''
select * from my_contacts
where
location = 'Grover''s Mill'
    ''',
]

for sql in sqls:
    print(sql)
    cur.execute(sql)
    cur.execute('''select * from my_contacts''')
    for row in cur.fetchall():
        print(row)
cur.close()
con.close()

入出力結果(Terminal, Zsh)

% ./sample4.py   

select * from my_contacts
where
location = "Grover's Mill"

('アンダーソン', 'ジリアン', 'F', 'jill_anderson@breakneckpizza.com', '1980-09-05', 'テクニカルライター', 'カリフォルニア州パロアルト', '独身', 'カヤック乗り、爬虫類', '恋人、友達')

select * from my_contacts
where
location = 'Grover''s Mill'
    
('アンダーソン', 'ジリアン', 'F', 'jill_anderson@breakneckpizza.com', '1980-09-05', 'テクニカルライター', 'カリフォルニア州パロアルト', '独身', 'カヤック乗り、爬虫類', '恋人、友達')
%