Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old January 26th, 2004, 02:33 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
insert values into table MySQL ?

I have written a "BASH" script that sends the output of the "date" , "who" , and "top" commands to a file. Instead of writting to this file I would like to redirect the output of each command to a specified field in a table in MySQL. The Bash code is below, I half way know the insert commands for MySQL but I can always look at the " PDF Manual ".

Code:
#! /bin/bash
LOG=/home/BaShScr/testlogger-$(date +%Y$m%d).log #Name of File that is written
ERR=/home/BaShScr/testloggererr.log		   #Name of error file that is written

 if  [ ! -e $LOG ] # If Log file does not exist then it is created
  then {
   date 1> $LOG
   echo "" 1>> $LOG	# If the Log file has to be created then 
   who  1>> $LOG        # this block of code is executed
   echo "" 1>> $LOG
   #uptime 1>> $LOG
   top -b -n1 1>> $LOG
   echo Logged the Date, Logged on users, and System uptime ! # Letting the user know the file had to be created.
   read							      # Script is finished and waiting for ENTER
   clear						      # Clears the buffer
  }
 elif [ -e  $LOG ] # If Log file exists then with same name the file is appended
  then {
   echo "##################################################  ######" 1>> $LOG
   date 1>> $LOG
   echo "" 1>> $LOG
   who  1>> $LOG		# If file is getting appended then			
   echo "" 1>> $LOG		# This block of code is being executed
   top -b-n1 1>> $LOG
   echo Log file appended !   # Letting the user know the file already existed and was appended
   read				# Script is finished and wating for key stroke
   clear			# Gives user a nice clean slate
  }
 else
 {
  if [ ! -e $ERR ]; then
   {
   echo 2> $ERR
   echo  OOPS! Nothing Happened !# User will see this if all else fails
   echo  Check testlogerror.err file for information # Gives users place to look if they want to knwo the answer to above
   }
  elif [ -e $ERR ]; then
   {
   echo 2>> $ERR
   echo  OOPS! Nothing Happened !# User will see this if all else fails
   echo  Check testlogerror.err file for information # Gives users place to look if they want to knwo the answer to above
   }
  fi
 } 
fi
 


From the code above you can see what I am asking.

For example I am able to [ date 1>> $LOG] and instead I want to [ date 1>> INSERT INTO name_of_table, name_of_field, VALUES ]

thanks in advance for any help.

Reply With Quote
  #2  
Old January 26th, 2004, 02:31 PM
xs0 xs0 is offline
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Ljubljana, Slovenia
Posts: 760 xs0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: insert values into table MySQL ?

Something like this should work:

echo "INSERT INTO table_name(field1, field2) VALUES("`date`", "`uptime`")" | mysql -uusername -ppassword dbname

note that `command` (that's ``, not '' !) gets replaces by the output of that command. Substitute commands, fieldnames etc. as necessary

Reply With Quote
  #3  
Old January 27th, 2004, 12:34 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: insert values into table MySQL ?

I guess to make this more understandable::

I am trying to get a script written that grabs the output of these commands and sends it to the correct fields in the databse:

The way I have it written now the output of the commands is captured and sent to a log file. ( or you could say redirected)

I already have the database created and the table with the fields ( Date, Logged_on, Process_stats)

The database name is "testlogger " and the table name is "LOGS"

Hopefully this help you understand what I am looking for.

The point behind this is to make the insertion of the output from the commands into the databe automated when the script is run.

This means more coding but that is what I am trying to learn:
Plus in the end I don't have to manually insert the data evey time I want to update the log I am keeping of the date, users logged on, and list of processes.

Thanks again:: I hope this helps explain what I am asking ::

Reply With Quote
  #4  
Old January 27th, 2004, 06:48 AM
xs0 xs0 is offline
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Ljubljana, Slovenia
Posts: 760 xs0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: insert values into table MySQL ?

Well, then do this

echo "INSERT INTO LOGS(Date, Logged_on, Process_stats) VALUES ("`date`", "`who`", "`top -b -n `")" | mysql -uusername -ppassword testlogger

As you can see, echo outputs the SQL statement which gets passed to mysql proggy, which sends it to the database...

Reply With Quote
  #5  
Old January 27th, 2004, 07:45 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: insert values into table MySQL ?

Oh i see what you were trying to tell me eariler.
sorry about the confusion.
Thank you

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > insert values into table MySQL ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway