Index . 블로그창고 . Any comments?

\cat software

txt2sql

txt2sql/txt2sql

Sometimes I need to convert MS Access data or Excel to a PostgreSQL table. However, their export filters do not satisfy all my needs, so I made a small perl script to convert a CSV-like text file to an SQL file.

It reads a file with a separator, TAB by default, tries to determine data type for each field and its maximum width, then it prints the table definition and INSERT queries for PostgreSQL. You may want to review the CREATE definition for data type checking before feeding the output to a database. Simply by modifying data type in the definition, it can be easily converted to SQL for any other DBMS engines.

Here is a sample file (sample data.txt):

id	sample type (AB, CC)	location (km, km)
1	AB	23, 3
2	AB	1, 2
3	CC	40, 40

Running txt2sql "sample data.txt" prints the following:

create table "sample data" (
	"id" int,
	"sample type (AB, CC)" varchar(2),
	"location (km, km)" varchar(6)
);

insert into "sample data" values('1', 'AB', '23, 3');
insert into "sample data" values('2', 'AB', '1, 2');
insert into "sample data" values('3', 'CC', '40, 40');

All the works in this site except software and copyrighted materials by others (e.g., 문학) are licensed under a Creative Commons License. 소프트웨어 및 문학과 같이 다른 이에게 저작권이 있는 작품을 제외하고 모두 크리에이티브 커먼즈 라이센스를 따른다.
Thu Mar 28 17:17:58 2024 . XHTML . CSS (lightbox.css is not part of Uniqki. ;-) . Powered by Uniqki!
refresh . edit . loginout . index