前言
sqlite3 提供了较轻便的数据库操作,速度非常快,也比较稳定,在嵌入式产品中用的非常广泛,但嵌入式产品往往由于不稳定性因素非常多,备份是必不可少的,直接拷贝 .db 文件并不是太好的主意,所以引出本文所要讲的主题:sqlite3 导入/导出sql 语句。
导出
root@CATON:~# sqlite3 db/boardsprofile.db
sqlite version 3.6.0
Enter ".help" for instructions
Enter sql statements terminated with a ";"
sqlite> .tables
A04Profile BPMProfile HDERProfile LNBProfile VMUX
A13Profile CAMProfile HDESLProfile LNBTProfile router_table
A22Profile DDOProfile IPASIINProfile MDOProfile system_profile
A31Profile DSDE1VProfile IPASIProfile MUXProfile
A40Profile DSDEProfile IPMOutProfile MVProfile
ADOProfile HDDOProfile IPMProfile P1
ASDEProfile HDELProfile LNBBissProfile P6
sqlite>
我使用的sqlite版本是 3.6.0,其他也是类似的,通过.tables 可以看到所有的表类型。
sqlite> .dump
BEGIN TRANSACTION;
CREATE TABLE DDOProfile (key varchar(255) PRIMARY KEY,value text);
INSERT INTO "DDOProfile" VALUES('boardType','DDOProfile');
INSERT INTO "DDOProfile" VALUES('main_input_num','0');
INSERT INTO "DDOProfile" VALUES('main_output_num','0');
INSERT INTO "DDOProfile" VALUES('sub_input_num','1');
INSERT INTO "DDOProfile" VALUES('sub_output_num','0');
INSERT INTO "DDOProfile" VALUES('sub_in_0','1');
INSERT INTO "DDOProfile" VALUES('PlaySrc0','');
INSERT INTO "DDOProfile" VALUES('Default_Pict0','3');
INSERT INTO "DDOProfile" VALUES('AVSync','0');
INSERT INTO "DDOProfile" VALUES('Decoder_WorkMode','0');
INSERT INTO "DDOProfile" VALUES('Output_Format','3');
INSERT INTO "DDOProfile" VALUES('Aspect_Ratio','0');
INSERT INTO "DDOProfile" VALUES('AR_Convert','0');
INSERT INTO "DDOProfile" VALUES('Aud_Mute','0');
INSERT INTO "DDOProfile" VALUES('Aud_Level','32');
INSERT INTO "DDOProfile" VALUES('Aud_STCOffset','0');
INSERT INTO "DDOProfile" VALUES('Aud_Buff','0');
INSERT INTO "DDOProfile" VALUES('Aud_Mode','0');
INSERT INTO "DDOProfile" VALUES('Aud_DigitalOut','0');
INSERT INTO "DDOProfile" VALUES('Aud2_Mute','0');
INSERT INTO "DDOProfile" VALUES('Aud2_Level','32');
INSERT INTO "DDOProfile" VALUES('Aud2_STCOffset','0');
INSERT INTO "DDOProfile" VALUES('Aud2_Buff','0');
....
.dump 命令就是导出了,这里直接导出到输出了,而且非常多。导出到文件将是这样的:
sqlite3 db/boardsprofile.db ".dump" > /var/ftp/profile.sql非常完整的整个整个数据库的sql语句。
导入
sqlite3 test.db ".read /var/ftp/profile.sql"
非常简单了。结果如下:
root@CATON:~# sqlite3 test.db ".read /var/ftp/profile.sql"
root@CATON:~# sqlite3 test.db
sqlite version 3.6.0
Enter ".help" for instructions
Enter sql statements terminated with a ";"
sqlite> .tables
A04Profile BPMProfile HDERProfile LNBProfile VMUX
A13Profile CAMProfile HDESLProfile LNBTProfile router_table
A22Profile DDOProfile IPASIINProfile MDOProfile system_profile
A31Profile DSDE1VProfile IPASIProfile MUXProfile
A40Profile DSDEProfile IPMOutProfile MVProfile
ADOProfile HDDOProfile IPMProfile P1
ASDEProfile HDELProfile LNBBissProfile P6
sqlite>
profile.sql 这个文件可以方便的导入到 MysqL,sql server中,任何数据库皆通用了。
用处
让用户直接下载db文件并不是一个很好的决定,用户备份,我比较倾向于,保存sql语句即可。
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
小编个人微信号 jb51ccc
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!