dbTalk Databases Forums  

how to test if record exists from bash

comp.databases.mysql comp.databases.mysql


Discuss how to test if record exists from bash in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Cat 22
 
Posts: n/a

Default Re: how to test if record exists from bash - 03-16-2010 , 10:42 PM






Lennart wrote:

Quote:
On 16 mar, 06:21, Cat 22 <ca... (AT) invalid (DOT) org> wrote:
Cat 22 wrote:
I have a bash script that parses a text file and adds records to
a mysql database. Each row has a unique date field and time
field. I want to be sure i dont add a record that is already in
there so for each record i am about to add i need to verify that
no other record has the same date and time field values.
The text file i am parsing changes every 2 hours and contains
about 2 days worth of data, each record represents a 5 minute
interval. Every 2 hours the top X number of lines are discarded
and a new ones added at the bottom. Sometimes records are missed
so the count of bottom records added might not be the same as
the number of top records removed. So you can see it would be
easy to screw up and add the same record twice. (The text file
isnt under my control) Basically i need to do this: "if not
exist record where date and time are the same as what i am about
to insert then go ahead and add this new record"
How can i do that from within a bash bash script? Is there a
simple "Record exists" function?
Thanks
Cat22

Thanks for all the replies, here's what i came up with:
result=$(mysql -uxxxx -pyyyy database_name -e "SELECT COUNT(*) AS
NUMBER FROM $table WHERE Date='$adj_date' and Time='$adj_time'"
echo "$result"|grep -v NUMBER
This gives me a number of rows with that date and time, normally a
1 or a 0
adj_date and ad_time are reformatted dates and times with the date
reformatted to YYYY-MM-DD and time is reformatted from the totally
bizarre input of 6:05p 12:30a formats into the standard HH:MM
format. I am a total newbie in mysql but i am finding it
fascinating and wow, is it fast, compared to parsing stuff and
adding columns with scripts I'm combing the web for mysql reading
material Thanks
Cat22

I've got the impression that - you believe - it is not a big
problem for you, but you should be aware that it is possible to add
a row between your query above and the actual insert. You might
want to add concepts like transaction and integrity in your quest
for reading materials.

/Lennart
Right, i can see where if multiple programs or users were working on
the database this could be a problem. In my case its only me, so this
will suffice. I will read up on transactions tho
Thanks
Cat22

Reply With Quote
  #12  
Old   
Cat 22
 
Posts: n/a

Default Re: how to test if record exists from bash - 03-16-2010 , 10:49 PM






Cat 22 wrote:

Quote:
I have a bash script that parses a text file and adds records to a
mysql database. Each row has a unique date field and time field. I
want to be sure i dont add a record that is already in there so for
each record i am about to add i need to verify that no other record
has the same date and time field values.
The text file i am parsing changes every 2 hours and contains about
2 days worth of data, each record represents a 5 minute interval.
Every 2 hours the top X number of lines are discarded and a new
ones added at the bottom. Sometimes records are missed so the count
of bottom records added might not be the same as the number of top
records removed. So you can see it would be easy to screw up and
add the same record twice. (The text file isnt under my control)
Basically i need to do this: "if not exist record where date and
time are the same as what i am about to insert then go ahead and
add this new record"
How can i do that from within a bash bash script? Is there a simple
"Record exists" function?
Thanks
Cat22
I probably should explain that this is a database of weather
conditions read from one weather station. The output is from Davis
WeatherLink software which logs data to a text file. I'm capturing
that data in a database and will use it to develop a web page of
current conditions with the ability to move around in time. So for
example, I could get heating degree days for march in several years,
or rain for the month of june, or find things like what was the
heaviest rain storm in the last x years.
My first step in this is to be able to capture the data in a way that
makes retrieval easy and the mysql database is just the ticket
Thanks
Cat22

Reply With Quote
Reply




Thread Tools
Display Modes

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.