2025年4月14日 星期一 乙巳(蛇)年 正月十五 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 软件应用 > 数据库 > 其它

sqlite数据库的导出与导入

时间:08-27来源:作者:点击数:27
CDSY,CDSY.XYZ

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
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐