LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2005, 09:42 AM   #1
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Rep: Reputation: 0
Import Database Function Script Using Perl??


Hello,

I need to build a perl script that will read a user provided text file delimited by <tab> and 'drop' info from 'category' mysql table,then rebuild the same tables based on the imported data from the user text file.
See the sample user text file :cat.txt

-------------------------------------------------------------------------
TYPE<tab>catid<tab>catname<tab>catdescription<tab>CataccesCost
__________________________________________________________________________
CATEGORY<tab>1<tab>Computers<tab>Computer related vacancies<tab>30
CATEGORY<tab>2<tab>marketing<tab>Clerical and assistance vacancies<tab>30
CATEGORY<tab>3<tab>human resource<tab>hr related vacancies<tab>30
___________________________________________________________________________
Any help greatly appreciated.

DominiQue
 
Old 01-11-2005, 07:48 AM   #2
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Original Poster
Rep: Reputation: 0
Any perl guru willing to help??
 
Old 01-11-2005, 08:42 AM   #3
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/usr/bin/perl 

$db = "category";

$sql = "INSERT INTO $db (catid,catname,catdescription,CataccesCost) ";
$sql .= "VALUES('%s','%s','%s','%s')\n";

while(<>) {
    if(/CATEGORY/) {
        @values = split /\t/;
        printf($sql, $values[1], $values[2], $values[3], $values[4]);
    }
}
save it as convert_sql.pl, then (say myfile is the file with separate tab field) :

cat myfile | convert_sql.pl > myfile.sql
Then use any mysql client to connect to mysql server and use myfile.sql as
source script.

mysql> source myfile.sql

Last edited by Cedrik; 01-11-2005 at 08:55 AM.
 
Old 01-13-2005, 09:05 AM   #4
domquem
LQ Newbie
 
Registered: Jul 2004
Location: WASHINGTON,DC AREA
Distribution: SUSE LINUX 9.1
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks for the great help Cedrik at least i have something thats up and running even though it needs some litlle tweaks which i believe i can be able to handle.

Thanks again !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to import arguments to system() function skie_knite007 Programming 5 03-25-2005 08:49 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
Exporting MySQL error when trying to Import to another database. strez4prez Linux - Software 0 05-28-2004 11:43 AM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
Import records to MySQL Database from a shell script chrisk5527 Linux - General 12 03-24-2004 09:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration