MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_NextPart_01D2E0B7.FD9122F0" このドキュメントは単一ファイル Web ページ (Web アーカイブ ファイル) です。お使いのブラウザー、またはエディターは Web アーカイブ ファイルをサポートしていません。Windows? Internet Explorer? など、Web アーカイブをサポートするブラウザーをダウンロードしてください。 ------=_NextPart_01D2E0B7.FD9122F0 Content-Location: file:///C:/B1341C72/db2.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="shift_jis"
情報シス=
eム工学特論 (Advanced Information System Engineering)
概要 Abstract
今日の授=
ニは、SQL プログラムの入門である。この授業では Python 言語を使う。
Today' s topic is
introduction to SQL program. We also use Python.
&nb=
sp;
■ データベース名 database name
データベ=
[ス名は、データベース接続時に、使用したいデータベースを指定するのに使われる=
A database name is =
used to
specify the database to be used when database connection.
■ テーブル定義 Table Definition
テーブル=
シ、属性名の並び、各属性のデータ型、各属性の制約(たとえば主キー)などを記述=
すること
Table definition is=
to
describe a table names, a list of attribute names, a data type of each attr=
ibute,
constraints on a relational database systems.
■ SQL のData 型 (SQL D=
ata
Types)
代表的な=
烽フを下にまとめる。The followings are important SQL data typ=
es.
l NULL: 空値=
(a NULL
value)
l INTEGER=
: 符号付きの整数 (signed integer)
l REAL: 浮動小数点値 (floating point value)
l TEXT: 文字列=
(text
string).
■ 主キー (Primary Key)
テーブル=
フタップルを唯一に識別するのに使える属性あるいは属性の組のうち極小なものをキ=
ーという.その中で,データベース管理者が,データベースの管理上最も適当と判=
fし,かつ,空値 (NULL) をとることがありえないものを,リレーション=
スキーマの設計時に選んだものが主キーである.
Key is a list of m=
inimum
attributes that can identify each line in a table. A primary key is the one=
of
the keys of a table that is selected by a database administrator and must n=
ot
have NULL value.
■ リレーション・スキーマ(スキー=
}ともいう)(Relati=
on
schema)
Rをテーブル名,A1, A2,
…, An を属性名とするとき, リレーションスキーマを「R(=
A1, A2, …, An)」のように書く.
The relation schem=
a of a
table is R(A1, A2, …, An) if the table name is R and att=
ribute
names are A1, A2, … and An.
■ SQL 問い合わせ (SQL Query)
SQL 問い合わ=
ケは,リレーショナルデータベースに格納された1つまたは複数のテーブルを使う.例えば,次の=
ように書くと,m 個のテーブル T1, T2, ..., Tm の直積集合から条件 <expression> <=
/span>を満足す=
骰sのみを選び,そうして出来たテーブルの属性 A1, A2, ..., An を出力するという意味になる.
A SQL query use a =
single or
multiple tables stored in a relational table. The evaluation result of the
following SQL query is a list of A<=
/i>1, A2, ..., An of the lines in the Cartesian product of T1, T2, ..., Tm, and the lines satisfy the cond=
ition
<expression>.
SELDCT=
A1, A2,
..., An
FROM T1, T2, ,,,, Tm
WHERE<=
span
style=3D'mso-spacerun:yes'> <expression>
■ カーソル Cursor
カーソル=
ヘテーブルの各行を指し示すために使われる
A cursor is used as=
a pointer
to a row of a relational table
■ 条件 (Condi=
tion)
テーブル=
シとドット「.」と列名=
フ並びを列の修飾名と呼ぶ.例えば,テーブルR の列 A の修飾名は =
R.A である.=
カ字定数は,'X' のように,シングルクォーテーションマーク「=
'」で囲む=
D比較演算子は =3D,=
>, <, >=3D, <=3D, <> の6種類がある.探索条件は「R.A > 20」のように,列の修飾名と比較演=
Z子と定数の並びである.扱うテーブルが1つのときは,列の修飾名の代わりに列名を使う=
ことができる(「A > =
20」のよう=
ノ).条件はand で連結することができる.
A sequence of a ta=
ble name,
a dot (.) and an attribute nam=
e is
called a qualified attribute name. For example, the qualified attribute nam=
e of
attribute A in table R is R.A. A character is surrounded by
single-quotation marks, such as 'X=
'.
There are six comparison operators. They are =3D, >, <, >=3D, <=3D and <>. A condition is a sequence of one qualified attribute
name, one comparison operator and one constant value such as R.A > 20. Attribute names can =
be
used instead of qualified attribute names when a SQL query includes only one
table name. Operator and can b=
e used
to build a condition from more-than-one conditions.
=
演習 2=
(Exerc=
ises 2)
前準備 (preparation)
・=
pythonxy のインストール &n=
bsp; (install
pythonxy)
・=
spyder のインストール <=
/span> (install spyder)
・=
iris.csv (データファイル) (a data file
iris.csv)
■ ステップ<=
/span>1 (Step 1)
以下の手順で Spyder=
を起動し=
ネさい
Launch the Spyder software
スタート=
<jューの「Anacond=
a3
(64bit)」の下の「spyder<=
/span>」を起動=
キる.右下に IPython コンソール・ウインドウがある.
Launch “spyder” in the “Anaconda3 (64b=
it)” in a
start menu. There is an IPython console windows in right lower.
■ ステップ<=
/span>2 (Step 2)
IPython コンソールで、SQLite3=
データベースに接続する
connect to a SQLite3 database using IPython console
この演習ではSQLite3=
データベース名は hoge2.sqlite
SQLite3 database name is 'hoge2.sqlite'
import sqlite3
c =3D sqlite3.connect('hoge=
2.sqlite')
■ ステップ<=
/span>3 (Step 3)
Python と埋め込みSQLを使い、テーブル定義を行う.
Define a table usin=
g Python
and embedded SQL.
@ テーブル定義のSQL プログラムを書き、変数 sql に格納
describe a SQL program to define a relational table and store int=
o a
variable
複数行に=
たるユニコード文字列を使いところでは、「u""" ・・・ """」のように書く.
sql =3D u"""=
create table iris (
id integer primary key,
sepal_width real,
petal_length real,
peral_width real,
specied integer );
"""
A=
オブジェクトsqlを確認する=
Examine the object “sql”.
sql
B=
オブジェクトsqlに格納されたSQLプログラムを評価させる
Evaluate the=
SQL program
stored in object “sql”
c.execute(sql)
■ ステップ4 (Step 4)
CSVファイルをテーブルに格納する
&n=
bsp;
Examine the correct CSV file name)
l=
Windows でのファイル名「C:\iris.csv」は、Python のプログラム中では「'C:\\ir=
is.csv'」のよう=
ノ書く
l
読み込みたいCSVファイル=
フ先頭行に、「id, sepal_length, sepal_width, petal_leng=
th,
petal_width, species」のように属性名リストがある場合には「header=
=3D0」を付け=
import pandas as pd
x =3D pd.read_csv( 'c:\\iri=
s.csv',
header=3D0 )
l=
「=
for r i=
n x:」は x の各行について繰り返すという Python プログラム
l=
「=
insert =
into
iris values」は、テーブルに1行挿入す=
驍ニいうSQLプログラム=
l
「r[0], r[1], r[2], r[3], r[4], r[5]」は、もとのCSVデータファイルの0列目、1列目、2列目、3列目、=
4列目、5列目を使うという意味
■ ステップ5 (Step=
5)
print (t)
■ ステップ6 (Step=
6)
条件に合致する行を読みだす
print (t)
=
=
=
=
=
■ ステップ7 (Step=
7)
SQLite 3 データベースに接続していたのを切断<=
/b>