開発環境
- OS X Mavericks - Apple、たまにFreeBSD 10(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4(プログラミング言語)
- SQLite (RDBMS(Relational Database Management System))
Head First SQL ―頭とからだで覚えるSQLの基本 (Lynn Beighley(著)、 佐藤 直生 (監訳)、 松永 多苗子 (翻訳)、オライリージャパン)の6章(高度なSELECT文: 新たな目でデータを見る)、エクササイズ(p.255)を解いてみる。
エクササイズ(p.255)
コード(BBEdit, Emacs)
sample255.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import sqlite3
connection = sqlite3.connect('chapter6.sqlite')
cursor = connection.cursor()
def p_all(table):
cursor.execute("""SELECT * FROM {0}""".format(table))
print(tuple(map(lambda header: header[0], cursor.description)))
p(table)
def p(msg):
print(msg)
for row in cursor.fetchall():
print(row)
cursor.execute("""
CREATE TABLE my_table(
test_char VARCHAR(10)
)""")
for x in [0, 1, 2, 3, 'A', 'B', 'C', 'D', 'a', 'b', 'c', 'd', '!', '@', '#',
'$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '[', ']', '{',
'}', ';', ':', "''", '"', '\\', '|', '`', '~', ',', '.', '<', '>',
'/', '?', 'A1', 'A 1', '日本語', 'カタカナ', 'ひらがな']:
cursor.execute("""
INSERT INTO my_table
VALUES(
'{0}')""".format(x))
cursor.execute("""
INSERT INTO my_table
VALUES(
NULL)""")
p_all('my_table')
cursor.execute("""
SELECT * FROM my_table
ORDER BY test_char
""")
p("my_table")
connection.commit()
connection.close()
入出力結果(Terminal, IPython)
$ ./sample255.py
('test_char',)
my_table
('0',)
('1',)
('2',)
('3',)
('A',)
('B',)
('C',)
('D',)
('a',)
('b',)
('c',)
('d',)
('!',)
('@',)
('#',)
('$',)
('%',)
('^',)
('&',)
('*',)
('(',)
(')',)
('-',)
('_',)
('+',)
('=',)
('[',)
(']',)
('{',)
('}',)
(';',)
(':',)
("'",)
('"',)
('\\',)
('|',)
('`',)
('~',)
(',',)
('.',)
('<',)
('>',)
('/',)
('?',)
('A1',)
('A 1',)
('日本語',)
('カタカナ',)
('ひらがな',)
(None,)
my_table
(None,)
('!',)
('"',)
('#',)
('$',)
('%',)
('&',)
("'",)
('(',)
(')',)
('*',)
('+',)
(',',)
('-',)
('.',)
('/',)
('0',)
('1',)
('2',)
('3',)
(':',)
(';',)
('<',)
('=',)
('>',)
('?',)
('@',)
('A',)
('A 1',)
('A1',)
('B',)
('C',)
('D',)
('[',)
('\\',)
(']',)
('^',)
('_',)
('`',)
('a',)
('b',)
('c',)
('d',)
('{',)
('|',)
('}',)
('~',)
('ひらがな',)
('カタカナ',)
('日本語',)
$
0 コメント:
コメントを投稿