0% found this document useful (0 votes)
72 views

Python Training Course VIII: Relational Database

This document provides an overview and introduction to relational databases in Python. It discusses SQLite, an embedded SQL database that is widely used for local application storage. It then covers the DB-API standard interface for interacting with databases in Python. SQLAlchemy is introduced as a popular toolkit that provides an object-relational mapper (ORM) for working with databases in Python. It allows defining database schemas and interacting with records as Python objects. The document demonstrates how to install and use SQLite, the DB-API, and SQLAlchemy for basic database operations like creating tables, inserting and selecting records in Python.

Uploaded by

Ruei Yu Zeng
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Python Training Course VIII: Relational Database

This document provides an overview and introduction to relational databases in Python. It discusses SQLite, an embedded SQL database that is widely used for local application storage. It then covers the DB-API standard interface for interacting with databases in Python. SQLAlchemy is introduced as a popular toolkit that provides an object-relational mapper (ORM) for working with databases in Python. It allows defining database schemas and interacting with records as Python objects. The document demonstrates how to install and use SQLite, the DB-API, and SQLAlchemy for basic database operations like creating tables, inserting and selecting records in Python.

Uploaded by

Ruei Yu Zeng
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Python Training Course VIII

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
練習題

 請安裝 SQLite3 & DB Browser for SQLite

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

• The most popular cross-database Python library


• The Python SQL Toolkit and Object Relational Mapper

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.

zoo object was a


mid-level connection
between SQL and Python

取代前面的 SQL insert 語


取代前面的 SQL select 語


27
The Object-Relational Mapper (ORM)

magically creates the


database and table

28
The Object-Relational Mapper (ORM)
create a session to
talk to the database

check the db file

29
練習題

 Book P.215
 Things to Do 8.10

30

You might also like