![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
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 |
#12
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |