Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate SQL-table: resource(s) with shell scripts

Status
Not open for further replies.

Ladyluck

Technical User
Mar 8, 2002
15
IS
I am interested in obtaining/writing a simple script which will populate a table in a database (SQLite or MySQL) with data from a text file. The text file contains several lines of data with many individual values per line.
I thought it would be easy to find scripts on the could use as templates or general guidelines but my googling has been unsuccessful.
A pointer to a useful resource will be much appreciated.
LL
 
Hello, Ladyluck!

Suppose that you have table tbl1:

Code:
create table tbl1 (
    id integer primary key,
    dsc varchar,
    dte date
);

You also have data in a csv text file (semicolon is a field separator):

Code:
1;1st row;20060415
2;2nd row;20060416
2;3rd row;20060417

Suppose that you must convert date.

Awk script can be:

Code:
BEGIN { FS = ";" }

{
    dte = substr($3, 0, 4) "-" substr($3, 5, 2) "-" substr($3, 7)
    print "insert into tbl1 values (" $1 ", " $2 ", " dte ");"
}

Output is:

Code:
insert into tbl1 values (1, 1st row, 2006-04-15);
insert into tbl1 values (2, 2nd row, 2006-04-16);
insert into tbl1 values (2, 3rd row, 2006-04-17);

Maybe this example gives you an idea.

Jesus loves you. Bye.

KP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top