【sqlite3】基本使用



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.

n/a

1.1.2 安装
  1. 将下载下来的 sqlite-tools-win32-x86-***.zip 解压。
    n/a

  2. 将解压出来的三个文件:sqlite3.exe, sqlite3_analyzer.exe, sqldiff.exe 剪贴到 PATH 中去。

    我个人习惯在 C:\ 盘下创建一个 bin\ 文件夹,并且将其添加到了 PATH 中去。
    在这个 C:\bin\ 文件夹下,我用来放置和 LINUX 下类似的命令行工具。
    仅供参考。

    • 将以上三个文件放在 C:\bin\ 文件夹中:
      n/a

    • 无需配置 PATH,已存在在 PATH 中的路径有:C:\windows\system32\ (即系统本身放置 cmd.exe 的位置),可以将上述三个文件放置在这个目录下即可。

  3. [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 650011

在 Windows 的 CMD 中 Query,可能出现中文显示乱码的情况(即使数据库内正确保存了 UTF-8 编码的中文字符),可以使用可视化软件(工具)开执行 SQL 语句,并显示结果:

n/a



4. 数据库命令对比

首先运行以下命令:

sqlite> .header on
sqlite> .mode column
SQLiteMySQL
.quit/.exitquit;
.tablesSHOW TABLES;
pragma table_info(’<tablename>’);DESC <tablename>;
--------


5. SQL 对比

SQLiteMySQL
autoincrementauto_increment
integerint

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:

typecomment
NULLThe value is a NULL value.
INTEGERThe value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
REALThe value is a floating point value, stored as an 8-byte IEEE floating point number.
TEXTThe value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
BLOBThe value is a blob of data, stored exactly as it was input.


Reference

  1. 《SQL权威指南》
  2. Using SQLite
  3. sqlite.org> pragma.html#pragma_encoding ?



  1. SQLite UTF-8 encoding ? ↩︎

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值