開発環境
- macOS Mojave - Apple
- Emacs (Text Editor)
- PostgreSQL (ORDBMS(object-relational database management system))
- Python 3.7 (プログラミング言語)
- psycopg2(パッケージ)
Head First SQL ―頭とからだで覚えるSQLの基本 (Lynn Beighley (著), 佐藤 直生 (監訳)、松永 多苗子 (翻訳)、オライリージャパン)の1章(データとテーブル - あらゆるものにふさわしい場所)、SQLマグネット(p. 29).を取り組んでみる。
コード(Emacs)
Python 3
#!/usr/bin/env python3 import psycopg2 as sql conn = sql.connect(database='gregs_list', user='kamimura') cursor = conn.cursor() _sql = ''' CREATE TABLE IF NOT EXISTS my_contacts ( email VARCHAR(50), birthday DATE, first_name VARCHAR(20), last_name VARCHAR(30), interests VARCHAR(100), seeking VARCHAR(100), status VARCHAR(20), profession VARCHAR(50), location VARCHAR(50), gender CHAR(1) ) ''' cursor.execute(_sql) conn.commit() cursor.close() conn.close()
入出力結果(Terminal, Jupyter(IPython))
$ psql -U kamimura psql (10.5) Type "help" for help. kamimura=# create database gregs_list; CREATE DATABASE kamimura=# \q $ ./sample1.py $ psql -U kamimura psql (10.5) Type "help" for help. kamimura=# \d Did not find any relations. kamimura=# \q $ psql -U kamimura gregs_list psql (10.5) Type "help" for help. gregs_list=# \d List of relations Schema | Name | Type | Owner --------+-------------+-------+---------- public | my_contacts | table | kamimura (1 row) gregs_list=# \d my_contacts Table "public.my_contacts" Column | Type | Collation | Nullable | Default ------------+------------------------+-----------+----------+--------- email | character varying(50) | | | birthday | date | | | first_name | character varying(20) | | | last_name | character varying(30) | | | interests | character varying(100) | | | seeking | character varying(100) | | | status | character varying(20) | | | profession | character varying(50) | | | location | character varying(50) | | | gender | character(1) | | | gregs_list=# \q $
0 コメント:
コメントを投稿