[901]sqlite数据库的导出与导入

时间:2022-07-26
本文章向大家介绍[901]sqlite数据库的导出与导入,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

SQLite 仅仅支持 ALTER TABLE 语句的一部分功能,我们可以用 ALTER TABLE 语句来更改一个表的名字,也可向表中增加一个字段(列),但是我们不能删除一个已经存在的字段,或者更改一个已经存在的字段的名称、数据类型、限定符等等。

改变表名 - ALTER TABLE 旧表名 RENAME TO 新表名 增加一列 - ALTER TABLE 表名 ADD COLUMN 列名 数据类型

SQLite 获取所有表名

SELECT name FROM sqlite_master where type='table' order by name;

通过sqlite3 test.db命令进入sqlite数据库的shell 操作:

1,导出数据库某个表:

# 先执行
.output table_name.sql
# 在执行
.dump table_name

如果是导出全部表:

直接 .dump

2,导入数据表:

.read table_name.sql

python 脚本:

1,导出表:

cmd = "sqlite3 db.sqlite3 '.dump table_name' > table_name.sql"
os.system(cmd)

2,导入表:

cmd = "sqlite3  db.sqlite3 '.read table_name.sql' "
os.system(cmd)

.help

执行“sqlite3.exe”,我们可能用到下面几个命令:
sqlite> .help
.dump ?TABLE? ...      Dump the database in an SQL text format
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.exit                  Exit this program
.help                  Show this message
.open ?--new? ?FILE?   Close existing database and reopen FILE
                         The --new starts with an empty file
.output ?FILENAME?     Send output to FILENAME or stdout
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.tables ?TABLE?        List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
sqlite>

直接导出csv文件

sqlite3  -csv -header vz3.db  "select * from t_city_domestic_all_new" > city.csv

参考:https://blog.csdn.net/kevin_weijc/article/details/78920593 https://blog.csdn.net/u013600225/article/details/53898697 https://www.jianshu.com/p/2980342c7be6 https://blog.csdn.net/yujianxiang666/article/details/46724923