Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Inserting values to database

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-13-2017
preema preema is offline
Registered User
 
Join Date: May 2017
Last Activity: 11 September 2017, 6:43 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Display Inserting values to database

hi ,

I'm new to Unix shell scripting.

I need help to insert read csv which has two columns -emp no and logged date.

csv file is a large file so i want to keep the insertion query in a separate .sql file.

csv file looks this:



Code:
empno | loggeddate
___________________
5666 , "2-mar-2017"
7888, "3-mar-2017"
......



Please help .


Thanks in advance.

Moderator's Comments:
Inserting values to database Please use code tags

Last edited by jim mcnamara; 05-13-2017 at 06:54 AM.. Reason: code tags; moved thread
Sponsored Links
    #2  
Old 05-13-2017
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 17 October 2017, 10:50 PM EDT
Location: NM
Posts: 11,212
Thanks: 567
Thanked 1,110 Times in 1,024 Posts
I would guess your db is oracle. What shell and OS (ksh, bash, linux, and so on) are you using? If you can help us to help you things will go very well.
Sponsored Links
    #3  
Old 05-13-2017
preema preema is offline
Registered User
 
Join Date: May 2017
Last Activity: 11 September 2017, 6:43 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

Yes,database is oracle.I'm using bash.

Thanks
    #4  
Old 05-13-2017
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 17 October 2017, 10:50 PM EDT
Location: NM
Posts: 11,212
Thanks: 567
Thanked 1,110 Times in 1,024 Posts
Do you know about using sqlldr? It is a lot faster than trying to write and run a sql file with thousands of insert statements, and is meant to do exactly what you want.

sqlldr will be in your PATH, if you have your environment variables set so that

Code:
sqlplus preema/password

connects you to the db you want.
loader1.dat is a control file in the current directory

Code:
load data
INFILE 'myinputfilename.csv'
INTO TABLE mytablename
APPEND
FIELDS TERMINATED BY ','
(empno,
 logged_date DATE "DD-MON-YYYY")

The log file shows errors. If there are errors this helps you to correct input
and remove the good lines that were entered. Then you can rerun.
Shell command:

Code:
sqlldr userid=preema/password control=loader1.dat log=loader.log

Sponsored Links
    #5  
Old 05-15-2017
preema preema is offline
Registered User
 
Join Date: May 2017
Last Activity: 11 September 2017, 6:43 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Hi ,
Thanks it works!!!

But in case I need to insert these data to a file say - master.log. what should be done?
Sponsored Links
    #6  
Old 05-15-2017
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 17 October 2017, 10:50 PM EDT
Location: NM
Posts: 11,212
Thanks: 567
Thanked 1,110 Times in 1,024 Posts
What do you mean by 'insert to a file' ? The csv file you used as the source of data is already a file isn't it?
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inserting script variables into database leshy93 Shell Programming and Scripting 1 12-01-2016 04:45 AM
Inserting values into database from an excel venkidhadha Shell Programming and Scripting 2 05-31-2016 06:35 AM
Inserting variable values in filename CAch Shell Programming and Scripting 14 04-05-2013 07:06 AM
Script for Logfile Inserting into the Database kgrvamsi Shell Programming and Scripting 7 04-21-2011 09:47 AM
help in inserting values in date format ali560045 Shell Programming and Scripting 3 02-04-2008 09:12 AM



All times are GMT -4. The time now is 09:07 AM.