数据库系统一般分为大型、中型、小型。其中大型常见的有 Oracle 、DB2 等,中型有 ms-sqlserver、MySQL 等,小型的有 access 、Sqlite3 等。本文就是给大家介绍其中的小型数据库 sqlite3 。
SQLITE3 之所以被称为嵌入式数据库,就是因为其开源(总代码行约 3 万行 C 语言代码)、轻量级(体积小、容量大2T)、可移植、易于集成等特点,特别适用于嵌入式平台。
Sqlite3安装
在线安装
在 Ubuntu 平台,直接使用 apt-get 命令进行安装
sudo apt-get install sqlite3
源码编译安装
下载源码
源码下载地址:https://www.sqlite.org/download.html
此处以 sqlite-autoconf-3460000.tar.gz 为例进行下载。
编译安装
上传下载的源码包到目标服务器,直接解压,进入解压目录后,编译安装即可(全部使用默认选项)
tar -zxf sqlite-autoconf-3460000.tar.gz
cd sqlite-autoconf-3460000/
./configure
make
sudo make install
安装测试
直接通过命令查看版本号即可验证:
[sheep@ubuntu2204:~/tools/sqlite-autoconf-3460000]$ sqlite3 --version
3.46.0 2024-05-23 13:25:27 96c92aba00c8375bc32fafcdf12429c58bd8aabfcadab6683e35bbb9cdebf19e (64-bit)
Sqlite3 的简单使用
创建数据库
创建数据库的命令为 sqlite3 xxx.db
,如创建并打开 test 数据库:sqlite3 test.db
:
[sheep@ubuntu2204:~/tools/sqlite-autoconf-3460000]$ sqlite3 test.db
SQLite version 3.46.0 2024-05-23 13:25:27
Enter ".help" for usage hints.
sqlite>
执行后会自动进入数据库管理界面,其中的 sqlite>
就是数据库终端提示符。
.help
会显示当前数据库所有的默认命令:
sqlite> .help
.archive ... Manage SQL archives
.auth ON|OFF Show authorizer callbacks
.backup ?DB? FILE Backup DB (default "main") to FILE
.bail on|off Stop after hitting an error. Default OFF
.cd DIRECTORY Change the working directory to DIRECTORY
.changes on|off Show number of rows changed by SQL
.check GLOB Fail if output since .testcase does not match
.clone NEWDB Clone data into NEWDB from the existing database
.connection [close] [#] Open or close an auxiliary database connection
.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
.dump ?OBJECTS? Render database content as SQL
.echo on|off Turn command echo on or off
.eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN
.excel Display the output of next command in spreadsheet
.exit ?CODE? Exit this program with return-code CODE
.expert EXPERIMENTAL. Suggest indexes