Ming

物无非彼 物无非是


  • 首页

  • 生信知识集

  • 一起踩坑

  • 归档

close

如何将表格文件转为 sqlite3 数据库文件

2020-08-18   |   Others     |  

sqlite3

平时工作中碰到的需求,需要将 tsv/csv 文件转为 sqlite3 数据库文件,本来一直想用 python 的数据库接口来实现这一目的,但经在网上的搜索,发现直接使用 sqlite3 的点命令更为方便,特此记录。

数据准备

1
for i in {1..10};do echo -e "$i\t${i}_${i}";done > test.tsv

数据库生成

  1. 连接数据库
1
2
# 如果没有 test.db, 则会自动生成
sqlite3 test.db
  1. 使用 sqlite3 的点命令生成表
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 查看数据库内已有的 table
.tables

# 设定为制表符输出
.mode tabs

# 有头,首行将作为 headr,把表格导入名为 head 的 table 中
.import test.tsv head
# 查看 header
.schema head
# 查看前5行
SELECT * FROM head LIMIT 5;
# 2	2_2
# 3	3_3
# 4	4_4
# 5	5_5
# 6	6_6


# 无头,把表格导入名为 nohead 的 table 中
CREATE TABLE nohead('A','B');
.import test.tsv nohead
# 查看 header
.schema nohead
# 查看前5行
SELECT * FROM nohead LIMIT 5;
# 1	1_1
# 2	2_2
# 3	3_3
# 4	4_4
# 5	5_5

# 退出数据库
.quit

备注

常用的点命令可使用 .help 查询,.help command可查看某一点命令的具体使用方法

#数据库# #SQL#
Centos7 中各种国内源的配置
HUGO 备忘
Ming Jia

Ming Jia

BioInformation Analyst

9
3
12
GitHub 微信公众号
  • 数据准备
  • 数据库生成
  • 备注
© 2009 - 2021 Ming
Powered by - Hugo v0.71.1
Theme by - NexT
0%