Overview
1. 准备
1.1 Windows
1.1.1 下载
widowns sqlite3 tool:
https://www.sqlite.org/download.html > sqlite-tools-win32-x86-***.zip
A bundle of command-line tools for managing SQLite database files, including the command-line shell program, the sqldiff.exe program, and the sqlite3_analyzer.exe program.
1.1.2 安装
-
将下载下来的 sqlite-tools-win32-x86-***.zip 解压。
-
将解压出来的三个文件:sqlite3.exe, sqlite3_analyzer.exe, sqldiff.exe 剪贴到
PATH
中去。我个人习惯在
C:\
盘下创建一个bin\
文件夹,并且将其添加到了PATH
中去。
在这个C:\bin\
文件夹下,我用来放置和 LINUX 下类似的命令行工具。
仅供参考。-
将以上三个文件放在
C:\bin\
文件夹中:
-
无需配置
PATH
,已存在在PATH
中的路径有:C:\windows\system32\
(即系统本身放置cmd.exe
的位置),可以将上述三个文件放置在这个目录下即可。
-
-
[Win] + [R] -> 键入
cmd
-> [Enter] 回车 -> cmd 窗口 -> 输入sqlite3
。如果进入了 sqlite3 命令模式中,则安装完成。注:当然,也可以在 cmd 窗口进入到解压后的目录中,直接运行 sqlite3.exe。
1.2 LINUX(Ubuntu)
安装:
$ sudo apt search sqlite3
[...]
sqlite3/bionic-updates,bionic-security 3.22.0-1ubuntu0.1 amd64
Command line interface for SQLite 3
[...]
$ sudo apt install sqlite3 -y
2. 使用
2.1 Windows
直接打开已存在的数据库文件使用:
> dir
... db.sqlite3
[...]
> sqlite3 db.sqlite3
SQLite version 3.28.0 2019-04-16 19:49:53
Enter ".help" for usage hints.
sqlite>
sqlite> .header on
sqlite> .mode column
sqlite> .tables
auth_group csdn_profile
auth_group_permissions csdn_user
auth_permission django_admin_log
[...]
sqlite> .exit
创建数据库(文件)和使用:
> dir
... db.sqlite3
[...]
> sqlite3 test.db
SQLite version 3.28.0 2019-04-16 19:49:53
Enter ".help" for usage hints.
sqlite> #### 创建数据库示例 ? ######
sqlite> CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
login VARCHAR,
username VARCHAR,
password VARCHAR
);
sqlite> pragma table_info('user');
cid name type notnull dflt_value pk
---------- ---------- ---------- ---------- ---------- ----------
0 id INTEGER 0 1
1 login VARCHAR 0 0
2 username VARCHAR 0 0
3 password VARCHAR 0 0
sqlite> .exit
> dir
... db.sqlite3
... test.db
[...]
>
2.2 LINUX(Ubuntu)
基本和 Windows 在使用上没有什么区别:
$ sqlite3 /<path>/<to>/db.sqlite3
...
sqlite> [...]
2.3 执行 sql 脚本文件
参考:
$ ls
db.sqlite3 test_query.sql test_query2.sql
$ ## 方式一
$ cat test_query.sql
.open db.sqlite3
.headers on
.mode column
SELECT * FROM user;
$ sqlite3 < test_query.sql
id login username password
---------- --------------------- ---------- ----------
1 joseph.lin@aliyun.com Joseph Lin 1234
$
$ cat test_query2.sql
SELECT * FROM user;
$ ## 方式二
$ sqlite3 db.sqlite3
sqlite> .headers on
sqlite> .mode column
sqlite>
sqlite> .read test_query2.sql
...
sqlite> .exit
$
3. 基本配置:数据编码等
3.1 编码:utf-8
Windows:
> sqlite3 test.db
[...]
sqlite> PRAGMA encoding; #### Show the encode
UTF-8
sqlite> PRAGMA encoding = "UTF-16"; #### set encode tobe UTF-16
sqlite> PRAGMA encoding = "UTF-8"; #### set encode tobe UTF-8
sqlite>
如果想要默认使用 CMD 终端的编码,并且该编码为 UTF-8,则在 CMD 中先运行 chcp 65001
1。
在 Windows 的 CMD 中 Query,可能出现中文显示乱码的情况(即使数据库内正确保存了 UTF-8 编码的中文字符),可以使用可视化软件(工具)开执行 SQL 语句,并显示结果:
4. 数据库命令对比
首先运行以下命令:
sqlite> .header on
sqlite> .mode column
SQLite | MySQL |
---|---|
.quit/.exit | quit; |
.tables | SHOW TABLES; |
pragma table_info(’<tablename>’); | DESC <tablename>; |
---- | ---- |
5. SQL 对比
SQLite | MySQL |
---|---|
autoincrement | auto_increment |
integer | int |
… | … |
5.1 数据类型
Storage Classes and Datatypes
Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:
type | comment |
---|---|
NULL | The value is a NULL value. |
INTEGER | The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. |
REAL | The value is a floating point value, stored as an 8-byte IEEE floating point number. |
TEXT | The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE). |
BLOB | The value is a blob of data, stored exactly as it was input. |
Reference
- 《SQL权威指南》
- Using SQLite
- sqlite.org> pragma.html#pragma_encoding ?