python
,SQLite
來自 老師的網站https://hackmd.io/@amostsai SQLite 資料庫
軟體屬於公共財(public domain)
SQLite可說是某種「美德軟體」(virtueware)
作者本人放棄著作權,而給使用SQLite的人以下的「祝福」(blessing):
支援大多數的SQL指令(下面會簡單介紹)
一個檔案就是一個資料庫。不需要安裝資料庫伺服器軟體
完整的Unicode支援(因此沒有跨語系的問題)。
速度很快
資料庫 -> 資料表 -> 資料列 -> 資料欄
資料庫為什麼需要正規化
(註一)
CREATE TABLE user (
'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
'name' TEXT NOT NULL,
'role' INTEGER NOT NULL DEFAULT 1
);
ALTER TABLE book RENAME TO books;
ALTER TABLE book ADD note text;
CREATE TABLE book2 AS SELECT id, name, price FROM book;
DROP TABLE book;
ALTER TABLE book2 RENAME TO book;
DROP TABLE book2;
select * from book;
select name, price from book;
select distinct name FROM book;
select * from book where name = '咖啡';
select * from book where name <> '咖啡';
select * from book where price > 150;
select * from book where price < 200;
select * from book where price >= 150;
select * from book where price <= 200;
select * from book where price between 150 and 190;
select * from book where name like '%咖%';
select * from book where id in (1, 2);
select * from book where price > 180 and name like '%美%'
select * from book where price > 180 or name like '%美%'
select * from book order by price;
select * from book order by price desc;
select * from user where role_id = 1 order by name desc;
insert into book (name, price) values ('美食之旅', 230);
update book set name = '美食之旅2', price = 250 where id = 4;
delete from book where name = '美食之旅2';
SELECT book.id, book.name, book.price, user.name from book inner join user on book.user_id = user.id;
SELECT user.id, user.name, user.account, role.name from user inner join role on user.role_id = role.id;
import sqlite3
conn = sqlite3.connect(r'C:\Users\install\Desktop\書籍管理.db')
books = conn.execute('select * from book')
for book in books:
print(book)
import sqlite3
conn = sqlite3.connect(r'C:\Users\student\Desktop\test.db')
add_user_sql = "insert into user (name, account, password) values ('{0}', '{1}', '{2}')"
check_user_sql = "select * from user where account='{0}' and password='{1}';"
# try:
# conn.execute(add_user_sql.format('mos', 'mos@kkk.com', 'qqqqqqqq'))
# conn.commit()
# except Exception as e:
# print(e)
try:
account = input('請輸入帳號: ')
password = input('請輸入密碼: ')
res = conn.execute(check_user_sql.format(account, password)).fetchone()
if(res == None):
print('帳號或密碼錯誤,請重新輸入')
else:
print('歡迎使用本系統')
except Exception as e:
print(e)
參考資料:
資料庫正規化練習檔
資料庫正規化 Database normalization
為何使用資料庫儲存資料時,需要先執行正規化?
說明資料庫正規化基本概念
資料庫正規化
關聯式資料庫系統的規劃
註一:資料庫正規化設計題目
SQLite Tutorial
SQLite學習手冊
SQLite 語法
Python 速查手冊12.6 資料庫 sqlite3
11.13. sqlite3 — DB-API 2.0 interface for SQLite databases
S20200530 YTC
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing