第二章 开始啦

第二章 开始啦

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
    

一般来说,二进制备份并没有SQL备份具有可移植性。

注意:无论备份做的多好,其效果取决于数据恢复方法。

获取数据库文件信息

使用sqlite3_analyzer工具。

posted on 2016-09-16 22:57  花老????  阅读(120)  评论(0)  编辑  收藏

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能