INSERT INTOtest(msg_to,TYPE,create_date) SELECT 'm3','c','2014-08-19 12:12:23';
5 Sqlite数据导出导入操作
5.1 数据导入.import命令
命令:.import FILE TABLE Import data from FILEinto TABLE
sqlite> .schema test
CREATE TABLE test(id integerdefault 0, username text);
sqlite>
注意1: 不要忘了开头的点.import
注意2: 这条语句不能用分号结束. 非SQL不需要分号结束.
注意3: 需要查看默认的分隔符separator. 必须一致,查看分隔符使用命令.show,如果不一致可能导致sqlite字段分割错误,利用”.separator”命令转换sqlite默认分隔符,比如.separator ","这一句就将分隔符改变为逗号,与预导入数据一致才能顺利导入,如下所示:
sqlite> .show
echo:off
eqp: off
explain: off
headers: off
mode:list
nullvalue: ""
output: stdout
separator: "|"
stats: off
width:
sqlite>
分隔符不是”|”,而文件test.csv的分隔符是”,”,所以要先转换分隔符,在导入:
sqlite> .separator ","
sqlite> .table
t2 test test_auto_incre
sqlite> .import test.csv test
导入成功,查看结果
sqlite> select * from test;
1,a1
2,a2
3,a3
4,a4
sqlite> .separator "|"
sqlite> select * from test;
1|a1
2|a2
3|a3
4|a4
sqlite>
5.2,数据导出.output命令
命令: .output FILENAME Send outputto FILENAME
然后输入sql语句, 查询出要导的数据. 查询后,数据不会显示在屏幕上,而直接写入文件.
结束后,输入
sqlite> .output stdout
将输出重定向至屏幕,如下所示:
[root@localhostsqlite-autoconf-3080403]#
[root@localhostsqlite-autoconf-3080403]# sqlite3 ti
SQLite version 3.8.4.32014-04-03 16:53:12
Enter ".help" forusage hints.
sqlite>
sqlite> .output test.sql
sqlite> select * from test;
sqlite> .exit
[root@localhostsqlite-autoconf-3080403]# more test.sql
1|a1
2|a2
3|a3
4|a4
[root@localhostsqlite-autoconf-3080403]#
5.3 使用5.2导出的文件恢复test表
先登录删除test表
[root@localhostsqlite-autoconf-3080403]# sqlite3 ti
SQLite version 3.8.4.32014-04-03 16:53:12
Enter ".help" forusage hints.
sqlite>
select * from test;
1|a1
2|a2
3|a3
4|a4
sqlite> delete from test;
sqlite> select * from test;
sqlite>
再导入test.sql文件的数据,如下所示
sqlite> .show
echo:off
eqp: off
explain: off
headers: off
mode:list
nullvalue: ""
output: stdout
separator: "|"
stats: off
width:
sqlite> .import test.sql test
sqlite> select * from test;
1|a1
2|a2
3|a3
4|a4
sqlite>
查询数据,显示导入表test成功。
参考文档: http://www.w3cschool.cc/sqlite/sqlite-installation.html