天天看点

第二章 开始啦

第二章 开始啦

Command-Line Program

shell mode

输入

sqlite3

即可。如果不指定数据库名字,sqlite会使用in-memory数据库。

可以使用CLP作为交互式的shell。所有输入会被作为query,以

.

开头的命令会作为CLP操作。

  • .help
  • .exit
  • .show

command-line mode

数据库管理

创建数据库

sqlite3 test.db

不会实际创建数据库文件,直到我们创建了table。这之前可以设置page size, character encodeing等不太容易更改的属性。

创建table

sqlite> create table test (id integer primary key, value text);

当一列属性为integer primary key时,这列会自增。

增加数据

sqlite> insert into test (id, value) values(1, 'eenie');
sqlite> insert into test (id, value) values(2, 'meenie');
sqlite> insert into test (value) values('miny');
sqlite> insert into test (value) values('mo');
           

读取数据

sqlite> .mode column
sqlite> .headers on
sqlite> select * from test;
           

前两行是为了格式化输出

id          value
----------  ----------
1           eenie
2           meenie
3           miny
4           mo
           

创建index和view

sqlite> create index test_idx on test (value);
sqlite> create view schema as select * from sqlite_master;
           

获取数据库schema信息

.table [pattern]

查询table和view的信息

sqlite> .tables
schema  test
           

.indices [table name]

查询indexes信息

sqlite> .indices test
test_idx
           

.schema [table name]

查询table或view的SQL定义。(DDL)

sqlite> .schema
CREATE TABLE test (id integer primary key, value text);
CREATE INDEX test_idx on test (value);
CREATE VIEW schema as select * from sqlite_master;
           

查询

sqlite_master

获取更多信息

sqlite> select type, name,tbl_name,sql from sqlite_master order by type;
type        name        tbl_name    sql
----------  ----------  ----------  -------------------------------------
index       test_idx    test        CREATE INDEX test_idx on test (value)
table       test        test        CREATE TABLE test (id integer primary
view        schema      schema      CREATE VIEW schema as select * from s
           

导出数据

使用

.dump

导出数据库对象为SQL格式到输出。默认输出为屏幕。可以通过

.output

命令改变输出。

sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE test (id integer primary key, value text);
INSERT INTO "test" VALUES(1,'eenie');
INSERT INTO "test" VALUES(2,'meenie');
INSERT INTO "test" VALUES(3,'miny');
INSERT INTO "test" VALUES(4,'mo');
CREATE INDEX test_idx on test (value);
CREATE VIEW schema as select * from sqlite_master;
COMMIT;
           

导入数据

  • SQL格式的数据库,使用

    .read

    命令。
  • 如果是CSV或其他被分割的数据,使用

    .import [file][table]

    命令。可以通过

    .separator

    命令更改分隔符。 ###格式化
  • .echo

    . 打开,执行一个命令,会输出命令。
  • .headers

    . on,输出包含列名。
  • .prompt

    更改提示语。
  • .nullvalue

    空值的显示
  • .mode

    设置输出的格式

    ascii column csv html insert line list tabs tcl

    其中一个

导出delimited数据

执行unattended命令(command-line 方式)

有两种方式

  1. 提供SQL命令
    sqlite3 test.db .dump
               
    sqlite3 test.db "select * from test"
               
  2. 通过重定向将文件作为输入。

    sqlite3 test2.db <test.sql

备份数据库

  1. .dump

    命令导出SQL格式。
  2. 制作二进制备份。先压缩,减少体积。
    sqlite3 test.db vacuum  
    cp test.db test.backup
               

获取数据库文件信息