SQLite 基础7

2021-12-01 17:27:11 浏览数 (1)

导入导出数据库

导出数据库

可以使用这种方式来将sqlite数据转化为SQL

代码语言:javascript复制
[root@h102 bin]# ./sqlite3 test.db .dump > test.sql
[root@h102 bin]# cat test.sql 
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE test ( id int primary key not null, name text );
INSERT INTO "test" VALUES(1,'hello');
INSERT INTO "test" VALUES(2,'hello');
INSERT INTO "test" VALUES(3,'hello');
INSERT INTO "test" VALUES(4,'hello');
INSERT INTO "test" VALUES(5,'hello');
COMMIT;
[root@h102 bin]# 

也可以定向的只dump一个表,但这个操作没法在shell中完成,只能在sqlite中完成

代码语言:javascript复制
sqlite> .dump test
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE test ( id int primary key not null, name text );
INSERT INTO "test" VALUES(1,'hello');
INSERT INTO "test" VALUES(2,'hello');
INSERT INTO "test" VALUES(3,'hello');
INSERT INTO "test" VALUES(4,'hello');
INSERT INTO "test" VALUES(5,'hello');
INSERT INTO "test" VALUES(12,'12');
INSERT INTO "test" VALUES(13,'www');
COMMIT;
sqlite> 

导入数据库

代码语言:javascript复制
[root@h102 bin]# ls
sqlite3  test.db  test.sql
[root@h102 bin]# ./sqlite3 test_tmp.db < test.sql 
[root@h102 bin]# ./sqlite3 test_tmp.db 
SQLite version 3.11.1 2016-03-03 16:17:53
Enter ".help" for usage hints.
sqlite> .tables 
test
sqlite> .schema test
CREATE TABLE test ( id int primary key not null, name text );
sqlite> .databases
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main             /usr/local/sqlite3.11/bin/test_tmp.db                     
1    temp                                                                       
sqlite> select * from test;
1|hello
2|hello
3|hello
4|hello
5|hello
sqlite> 

0 人点赞