Python Training Course VIII: Relational Database
Python Training Course VIII: Relational Database
Relational Database
Relational Database
SQLite
DB-API
SQLAlchemy
2
SQLite
3
SQLite 簡介
SQLite is zero-configuration, transactional SQL database engine
The code for SQLite is in the public domain and is thus free for
use for any purpose, commercial or private.
SQLite is the most widely deployed database in the world
SQLite 不是一個用戶端 / 伺服器結構的資料庫引擎,而是被整合在
用戶程式中
實現了大多數 SQL 標準
作為嵌入式資料庫,是應用程式 ( 如網頁瀏覽器 ) 在本地 / 用戶端儲
存資料的常見選擇
可能是最廣泛部署的資料庫引擎
4
SQLite is included by default in
Blackberry's BlackBerry 10 OS OpenBSD
Symbian OS FreeBSD
Nokia's Maemo illumos
Google's Android Oracle Solaris 10
Linux Foundation's MeeGo Apple’s OS
LG's webOS Tizen
NetBSD Windows 10
5
Programming language support
6
Summary
7
Installing SQLite for Windows
8
Download from SQLite official website
9
Creates c:\sqlite directory and extracts all files
10
add c:\sqlite path to environment variable
右鍵
Save then
Reboot
11
execute sqlite3 in command mode
12
Install DB Browser for SQLite
13
練習題
14
DB-API
15
SQL DDL
DDL (data definition language)
Handles creation, deletion, constraints, and permissions
for tables, databases, and uses
16
SQL DML
DML (data manipulation language)
Handles data insertions, selects, updates, and deletions
17
DB-API
cursor()
Create a cursor object to manage queries.
execute() and executemany()
Run one or more SQL commands against the database.
fetchone() , fetchmany() , and fetchall()
Get the results from execute .
18
Create table
in memory
19
Insert
20
Select
execute 之後再
fetch
into Tuple
order by
order by descending
where condition
close
21
練習題
Book P.215
Things to Do 8.6~8.9
22
SQLAlchemy
23
SQLAlchemy
The most popular cross-database Python library
The Python SQL Toolkit and Object Relational Mapper
The lowest level handles database connection pools,
executing SQL commands, and returning results. This is
closest to the DB-API.
Next up is the SQL expression language, a Pythonic SQL
builder.
Highest is the ORM (Object Relational Model) layer, which
uses the SQL Expression Language and binds application
code with relational data structures.
24
SQLAlchemy
$ pip install sqlalchemy
25
The engine layer
in memory
26
The SQL Expression Language It introduces functions
to create the SQL for
various operations.
27
The Object-Relational Mapper (ORM)
28
The Object-Relational Mapper (ORM)
create a session to
talk to the database
29
練習題
Book P.215
Things to Do 8.10
30