หลังจากไม่ได้ใช้ SQLite มา หลายปี พอมาเรียนอีกรอบ เลยมา Recap นิดหน่อย ตาม Code ด้านล่างเลยครับ
CREATE TABLE example_table (
id INTEGER PRIMARY KEY AUTOINCREMENT, -- Integer data type, auto-incremented
text_column TEXT, -- Text data type
integer_column INTEGER, -- Integer data type
real_column REAL, -- Real number (floating point) data type
blob_column BLOB, -- Blob data type for storing binary data
numeric_column NUMERIC, -- Numeric data type, can store numbers with decimal points
date_column DATE, -- Date data type (formatted as 'YYYY-MM-DD')
datetime_column DATETIME -- DateTime data type (formatted as 'YYYY-MM-DD HH:MM:SS')
);
-- Insert example data
INSERT INTO example_table (text_column, integer_column, real_column, blob_column, numeric_column, date_column, datetime_column)
VALUES (
'Example Text',
123,
456.78,
x'62696E6172792064617461', -- Binary data as hexadecimal string
789.01,
'2025-01-25',
'2025-01-25 04:25:25'
);
-- Query to verify the insert
SELECT * FROM example_table;ปกติแล้ว SQLite จะมีสิ่งทีเรียกว่า Storage Class เข้ามา Handle Data Type ตามนี้
- NULL
- INTEGER signed integer โดยเก็บในรูปตัวเลข หรือ 8 bytes
- REAL เก็บ floating point value จัดเก็บในรูปแบบ 8-byte IEEE floating point number.
- TEXT เก็บตัวอักษรตาม database encoding (UTF-8, UTF-16BE or UTF-16LE).
- BLOB The value is a blob of data, stored exactly as it was input.
สำหรับ DateTime เราสร้าง ใน SQLite มันมองเหมือนเป็น Enum แล้วมี Function Date แปลง โดยจริงๆแล้วเก็บในรูปแบบ
- TEXT กำหนด Format ตาม ISO8601 "YYYY-MM-DD HH:MM:SS.SSS"
- REAL เก็บเป็นจำนวนวัน แบบ Julian day numbers (JDN) โดยเริ่มจากหลังเที่ยง Greenwich November 24, 4714 B.C.
- INTEGER เก็บ Unix Time, the number of seconds โดยวันแรกจะนับจาก 1970-01-01 00:00:00 UTC.
อีก Type ที่น่าสนใจ Boolean มันจะมองในรูปแบบ INTEGER 0 / 1 และตัว SQLite จะแปลง "TRUE" and "FALSE"
ถ้าอยากดู Table Structure ให้ใช้คำสั่ง
PRAGMA table_info(customers);

Reference
Discover more from naiwaen@DebuggingSoft
Subscribe to get the latest posts sent to your email.


