我是postgresql和sql的新手。据我所知,手动为每个文件创建表格,然后使用副本将csv文件中的数据导入表格。如果我们有一堆csv文件,是否可以使用csv文件中的标题名称作为列名来导入和创建所有文件的表?
感谢
作为一个例子,使用我在评论中提到的程序:
--Shows table schema creation using Postgres dialect
csvsql -i postgresql --tables cp ~/cell_per.csv
CREATE TABLE cp (
line_id DECIMAL NOT NULL,
category VARCHAR NOT NULL,
cell_per DECIMAL NOT NULL,
ts_insert TIMESTAMP WITHOUT TIME ZONE,
ts_update TIMESTAMP WITHOUT TIME ZONE,
user_insert VARCHAR,
user_update VARCHAR,
plant_type VARCHAR,
season VARCHAR,
short_category VARCHAR
);
--Create table and import from file in one step
csvsql --db postgresql://aklaver:@localhost:5432/test --tables cp --insert ~/cell_per.csv
d cp
Table "public.cp"
Column | Type | Collation | Nullable | Default
----------------+-----------------------------+-----------+----------+---------
line_id | numeric | | not null |
category | character varying | | not null |
cell_per | numeric | | not null |
ts_insert | timestamp without time zone | | |
ts_update | timestamp without time zone | | |
user_insert | character varying | | |
user_update | character varying | | |
plant_type | character varying | | |
season | character varying | | |
short_category | character varying | | |
select count(*) from cp;
count
-------
68
(1 row)
这只是一个工具。您也可以使用Pandas的I/O工具。