開発環境
- OS X Mavericks - Apple、たまにFreeBSD 10(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
- SQLite (RDBMS(Relational Database Management System))
Head First SQL ―頭とからだで覚えるSQLの基本 (Lynn Beighley(著)、 佐藤 直生 (監訳)、 松永 多苗子 (翻訳)、オライリージャパン)の1章(データとテーブル: あらゆるものにふさわしい場所)、自分で考えてみよう(p.45)を解いてみる。
自分で考えてみよう(p.45)
コード(BBEdit, Emacs)
sample45.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- import sqlite3 connection = sqlite3.connect('gregs_list.sqlite') cursor = connection.cursor() cursor.execute("""DROP TABLE my_contacts""") cursor.execute(""" CREATE TABLE my_contacts( last_name VARCHAR(30) NOT NULL, first_name VARCHAR(20) NOT NULL, email VARCHAR(50) NOT NULL, gender CHAR(1) NOT NULL, birthday DATE NOT NULL, profession VARCHAR(50) NOT NULL, location VARCHAR(50) NOT NULL, status VARCHAR(20) NOT NULL, interests VARCHAR(100) NOT NULL, seeking VARCHAR(100) NOT NULL) """) connection.commit() connection.close()
入出力結果(Terminal, IPython)
$ ./sample45.py $ sqlite3 gregs_list.sqlite SQLite version 3.8.4.3 2014-04-03 16:53:12 Enter ".help" for usage hints. sqlite> PRAGMA table_info(my_contacts); PRAGMA table_info(my_contacts); 0|last_name|VARCHAR(30)|1||0 1|first_name|VARCHAR(20)|1||0 2|email|VARCHAR(50)|1||0 3|gender|CHAR(1)|1||0 4|birthday|DATE|1||0 5|profession|VARCHAR(50)|1||0 6|location|VARCHAR(50)|1||0 7|status|VARCHAR(20)|1||0 8|interests|VARCHAR(100)|1||0 9|seeking|VARCHAR(100)|1||0 sqlite> .help .help .backup ?DB? FILE Backup DB (default "main") to FILE .bail ON|OFF Stop after hitting an error. Default OFF .clone NEWDB Clone data into NEWDB from the existing database .databases List names and files of attached databases .dump ?TABLE? ... Dump the database in an SQL text format If TABLE specified, only dump tables matching LIKE pattern TABLE. .echo ON|OFF Turn command echo on or off .exit Exit this program .explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off. With no args, it turns EXPLAIN on. .header(s) ON|OFF Turn display of headers on or off .help Show this message .import FILE TABLE Import data from FILE into TABLE .indices ?TABLE? Show names of all indices If TABLE specified, only show indices for tables matching LIKE pattern TABLE. .load FILE ?ENTRY? Load an extension library .log FILE|off Turn logging on or off. FILE can be stderr/stdout .mode MODE ?TABLE? Set output mode where MODE is one of: csv Comma-separated values column Left-aligned columns. (See .width) html HTML <table> code insert SQL insert statements for TABLE line One value per line list Values delimited by .separator string tabs Tab-separated values tcl TCL list elements .nullvalue STRING Use STRING in place of NULL values .open ?FILENAME? Close existing database and reopen FILENAME .output FILENAME Send output to FILENAME .output stdout Send output to the screen .print STRING... Print literal STRING .prompt MAIN CONTINUE Replace the standard prompts .quit Exit this program .read FILENAME Execute SQL in FILENAME .restore ?DB? FILE Restore content of DB (default "main") from FILE .save FILE Write in-memory database into FILE .schema ?TABLE? Show the CREATE statements If TABLE specified, only show tables matching LIKE pattern TABLE. .separator STRING Change separator used by output mode and .import .show Show the current values for various settings .stats ON|OFF Turn stats on or off .tables ?TABLE? List names of tables If TABLE specified, only list tables matching LIKE pattern TABLE. .timeout MS Try opening locked tables for MS milliseconds .trace FILE|off Output each SQL statement as it is run .vfsname ?AUX? Print the name of the VFS stack .width NUM1 NUM2 ... Set column widths for "column" mode .timer ON|OFF Turn the CPU timer measurement on or off sqlite> .headers ON .headers ON sqlite> .mode column .mode column sqlite> PRAGMA table_info(my_contacts); PRAGMA table_info(my_contacts); cid name type notnull dflt_value pk ---------- ---------- ----------- ---------- ---------- ---------- 0 last_name VARCHAR(30) 1 0 1 first_name VARCHAR(20) 1 0 2 email VARCHAR(50) 1 0 3 gender CHAR(1) 1 0 4 birthday DATE 1 0 5 profession VARCHAR(50) 1 0 6 location VARCHAR(50) 1 0 7 status VARCHAR(20) 1 0 8 interests VARCHAR(100 1 0 9 seeking VARCHAR(100 1 0 sqlite> .quit .quit $
MySQL の DESC(DESCRIBE)、あるいは MySQLで SELECT * FROM table-name とした時のテーブルのヘッダー、行、列表示と似たように SQLite でも表示するのに、毎回 headers を ON、mode を column に設定するのを省略するために、home directory に次のファイルを追加。
~/.sqliterc
.mode column .headers ON
0 コメント:
コメントを投稿