SQLite Logo SQLite 是一个用 C 语言编写的开源、轻量级、快速、独立且高可靠性的 SQL 数据库引擎,它提供了功能齐全的数据库解决方案。SQLite 险些可以在所有的手机和计算机上运行,它被嵌入到无数人天天都在使用的浩繁应用程序中。
别的,SQLite 还具有稳定的文件格式、跨平台能力和向后兼容性等特点。SQLite 的开发者承诺,至少在 2050 年之前保持该文件格式不变。
本文将介绍 SQLite 的基础知识和使用方法。
SQLite 安装
在 SQLite 官方页面[1](https://sqlite.org/download.html) 下载得当你目标系统的压缩包。
下载并解压后,无论是在 Windows、Linux 照旧 Mac OS 系统上,你都可以得到一个 sqlite3 下令行工具。
以下是在 Mac OS 上解压后得到的下令行工具示例:
sqlite> .help .databases List names and files of attached databases .dbconfig ?op? ?val? List or change sqlite3_db_config() options .dbinfo ?DB? Show status information about the database .excel Display the output of next command in spreadsheet .exit ?CODE? Exit this program with return-code CODE .expert EXPERIMENTAL. Suggest indexes for queries .explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto .help ?-all? ?PATTERN? Show help text for PATTERN .hex-rekey OLD NEW NEW Change the encryption key using hexadecimal .indexes ?TABLE? Show names of indexes .mode MODE ?OPTIONS? Set output mode .open ?OPTIONS? ?FILE? Close existing database and reopen FILE .output ?FILE? Send output to FILE or stdout if FILE is omitted .quit Exit this program .read FILE Read input from FILE or command output .schema ?PATTERN? Show the CREATE statements matching PATTERN .show Show the current values for various settings .tables ?TABLE? List names of tables matching LIKE pattern TABLE .......
$ ./sqlite3 my_sqlite.db SQLite version 3.42.0 2023-05-16 12:36:15 Enter ".help" for usage hints. sqlite> .output backup.sql sqlite> .dump sqlite> .exit $ cat backup.sql PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE user(name text,age int); INSERT INTO user VALUES('aLang',20); INSERT INTO user VALUES('Darcy',30); INSERT INTO user VALUES('XiaoMing',40); CREATE INDEX user_name on user(name); COMMIT;