SQL - SQLite - ALTER文 - 過去の書き換え - 文字列関数、substr(substring)関数、length関数
Head First SQL ―頭とからだで覚えるSQLの基本、 Lynn Beighley(著)、 佐藤 直生(監訳)、 松永 多苗子(翻訳)、 オライリージャパンの 5章(ALTER文 - 過去の書き換え)、p.25(エクササイズ - 気楽にやってみよう)の解答を求めてみる。
コード
sample7.py
#! /usr/bin/env python3
import sqlite3
con = sqlite3.connect('gregs_list.db')
cur = con.cursor()
cur.execute('select location from my_contacts')
for row in cur.fetchall():
print(row)
sql = """
select substr(location, 9)
from my_contacts
"""
cur.execute(sql)
for row in cur.fetchall():
print(row)
sql = """
select length(location)
from my_contacts
"""
cur.execute(sql)
for row in cur.fetchall():
print(row)
sql = """
select instr(location, '州')
from my_contacts
"""
cur.execute(sql)
for row in cur.fetchall():
print(row)
cur.close()
con.close()
入出力結果(Terminal, Zsh)
% ./sample7.py
('カリフォルニア州パロアルト',)
('パロアルト',)
(13,)
(8,)
%