zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

SQLite 基础7

2023-03-20 15:36:42 时间

导入导出数据库

导出数据库

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

[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中完成

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> 

导入数据库

[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>