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

本文介绍了如何使用SQLite进行数据库操作,包括通过sqlite3命令行获取所有表名,导出和导入数据表,以及使用ALTERTABLE语句改变表名和增加列。同时,提供了Python脚本示例来实现表的导出和导入,并展示了.sqlite3命令的帮助信息。此外,还展示了直接将查询结果导出为CSV文件的方法。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值