play/box

SQLite 파일 구조

카고형 2023. 5. 21. 23:03
728x90

SQLite는 경량의 오픈 소스 관계형 데이터베이스 관리 시스템(RDBMS) 이고, 다른 RDBMS랑 다르게 서버가 필요 하지 않고, 사전 환경 설정 필요하지 않아 쉽고 가볍게 사용 가능 합니다. 

SQLite는 데이터는 접근하는 어플리케이션과 결합 하여 직접적으로 데이터베이스에 읽고/쓰기 작업을 수행 한다. 별도의 프로세서가 필요 하지 않아 가볍고, 다양한 운영체제, 언어에서 지원되며 이식성이 뛰어 나다.


SQLite 파일 구조

File Header
● ● ●
SChema Table
Page 2
Page 3
  
Page n

 

Header Page Hex

SQLite 파일에서 헤더 페이지에는 시그니처, 페이지 크기, 데이터베이스 크기등 메타 데이터가 저장되어 있는 곳이다

  0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF
0x00 Header String
0x01 Page Size             File Chage Counter Database Size
0x02 Free Page Offset Free Page Number Schema Cookie Schema Format number
0x03 Default Page cache Size Incremental Vacuum Settings Text Encoding User Version
0x04 Incremental Vacuum Mode                        
0x05 Record of Expansion  
0x06 SQLite Version Number                        

 

Test DB

test DB
testDB.zip
0.00MB

 

Header String : SQLite 형식을 알려줌

Header String

Page Size : 데이터베이스의  가장 작은 저장 다위인 페이지 하나의 크기 (16진수 값으로 저장 되어 있고) 크기는 4096(0x10000) Byte 이다. 

Page Size

Databae Size : DB 크기로 페이지 갯수로 저장 된다(헤더 페이지 포함)

Database Size

File Change Counter : 파일이 생성 된 후 수정 및 조작 횟수

File Change Counter

Text encode : 문자열 인코딩 유형의 값이다

Text encode

Incremental Vacuum Mode : 데이터 삭제시 해당 데이터 공간을 자동으로 정리 할지 정하는 값(00000000은 활성화 x)

Vacuum mode

테이블 갯수 : 0x68 

Tabe count

테이블 시작 주소 : 0x69~0x6A

테이블 스키마 시작주소

테이블 시작 주소에서 Ctrl + G 로 검색 하면 스키마 시작 하는곳으로 넘어감 테이블 스키마 확인 가능

테이블 스키마

 

SQLite B Tree Pages

Page Header Size : 0x1000

Page Header

Page Header를 제외한 페이지들은 Page header, Cell offset, Cell으로 구성 된다. 각각의 Cell offset은 Cell이 시작 하는 주소 값을 가지고 있다.

Offset Size Description
0x0 1 The one-byte flag at offset 0 indicating the b-tree page type.
A value of 2 (0x02) means the page is an interior index b-tree page.
A value of 5 (0x05) means the page is an interior table b-tree page.
A value of 10 (0x0a) means the page is a leaf index b-tree page.
A value of 13 (0x0d) means the page is a leaf table b-tree page.
Any other value for the b-tree page type is an error.
0x1 2 The two-byte integer at offset 1 gives the start of the first freeblock on the page, or is zero if there are no freeblocks.
0x3 2 The two-byte integer at offset 3 gives the number of cells on the page.
0x5 2 The two-byte integer at offset 5 designates the start of the cell content area. A zero value for this integer is interpreted as 65536.
0x7 1 The one-byte integer at offset 7 gives the number of fragmented free bytes within the cell content area.
0x8 4 The four-byte page number at offset 8 is the right-most pointer. This value appears in the header of interior b-tree pages only and is omitted from all other pages.

 

Page Header의 첫 바이트(00)는 Page flag로 페이지 유형을 나타낸다 0D는 leaf table b-tree page 이다

Page flag

 

01 ~ 02 는 블록의 주소 값을 갖는다.

블록 주소값

 

03 ~ 04은 해당 페이지의 레코드 갯수를 나타낸다

레코드 데이터 개수

 

05 ~ 06은 첫 byte가 시작되는 오프셋을 가지고 있다 현재 시작 페이지가 0x1000 +  0F42 를 하면 해당 페이지의  레코드 시작 주소로 이동 가능 하다 

시작 오프셋
시작 오프셋2

Leaf cell 구조

Leaf Page는 테이블의 레코드 데이터를 담고 있는 Leaf Cell 이 존재 하고 레코드의 길이와 Row ID를 포함하는 Cell Header 와 Record 로 구성된다.

Lengrh of Record

레코드 길이

Row ID : 각 행을 식별하는 값 (대부분 최근생성 된게 크다)

row

여기 까지가 Cell Header


Length of Data Hearder : 데이터 헤더 길이를 나타내는 값 (0x04) 값이 05 자기 포함 하여 5byte 만큼 길이를 같는다

data header
각 테이블 필드 길이

필드의 크기 계산 식

N≥12 and even (N-12)/2 Value is a BLOB that is (N-12)/2 bytes in length.
N≥13 and odd (N-13)/2 Value is a string in the text encoding and (N-13)/2 bytes in length. The nul terminator is not stored.

첫번쨰 1D 는 10진수로 바꾸면 29 이고 공식에 대입 하면 8 첫번째 필드에 문자열 'packgang'의 길이와 일치 하게 된다.


참고 주소

https://www.sqlite.org/fileformat.html

728x90