SQL - SQLite - SELECT - 天賦のデータ検索 - where句, and
Head First SQL ―頭とからだで覚えるSQLの基本、 Lynn Beighley(著)、 佐藤 直生(監訳)、 松永 多苗子(翻訳)、 オライリージャパンの 2章(SELECT - 天賦のデータ検索)、p.81(エクササイズ)の解答を求めてみる。
コード
sample7.py
#! /usr/bin/env python3
import sqlite3
con = sqlite3.connect('gregs_list.db')
cur = con.cursor()
sqls = [
'''
select email from my_contacts
''',
'''
select first_name, last_name, location from my_contacts
where location = 'カリフォルニア州パロアルト'
''',
'''
select first_name, last_name, location from my_contacts
where location = '日本'
''',
'''
select first_name, last_name, location from my_contacts
where location = 'カリフォルニア州パロアルト' and gender = 'F'
''',
'''
select first_name, last_name, location from my_contacts
where location = 'カリフォルニア州パロアルト' and gender = 'M'
''',
]
for sql in sqls:
print(sql)
cur.execute(sql)
for row in cur.fetchall():
print(row)
cur.close()
con.close()
入出力結果(Terminal, Zsh)
% ./sample7.py
select email from my_contacts
('jill_anderson@breakneckpizza.com',)
select first_name, last_name, location from my_contacts
where location = 'カリフォルニア州パロアルト'
('ジリアン', 'アンダーソン', 'カリフォルニア州パロアルト')
select first_name, last_name, location from my_contacts
where location = '日本'
select first_name, last_name, location from my_contacts
where location = 'カリフォルニア州パロアルト' and gender = 'F'
('ジリアン', 'アンダーソン', 'カリフォルニア州パロアルト')
select first_name, last_name, location from my_contacts
where location = 'カリフォルニア州パロアルト' and gender = 'M'
%