![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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? |
#3
| |||
| |||
|
|
On 15 mar, 05:57, Cat 22 <ca... (AT) invalid (DOT) org> 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? Have you considered a unique constraint on the table? |
#4
| |||
| |||
|
|
On Sun, 14 Mar 2010 22:20:20 -0700 (PDT), Lennart wrote: On 15 mar, 05:57, Cat 22 <ca... (AT) invalid (DOT) org> 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? Have you considered a unique constraint on the table? That (if you mean a primary key on the date/time) would also allow a completely transparent load, instead of a failure that has to be handled, via the ON DUPLICATE phrase. (Whether the new record, which is presumably the same values as the old but might not be, updates the existing row or is dropped is up to the OP. I'd personally kind of prefer the update on the grounds that it makes it easy to correct the system if prior records loaded are found to be in error, and records representing five minutes of time are cheap to process.) |
#5
| |||
| |||
|
|
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. |
#6
| |||
| |||
|
|
On Sun, 14 Mar 2010 22:20:20 -0700 (PDT), Lennart wrote: On 15 mar, 05:57, Cat 22 <ca... (AT) invalid (DOT) org> 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? Have you considered a unique constraint on the table? That (if you mean a primary key on the date/time) would also allow a completely transparent load, instead of a failure that has to be handled, via the ON DUPLICATE phrase. (Whether the new record, which is presumably the same values as the old but might not be, updates the existing row or is dropped is up to the OP. I'd personally kind of prefer the update on the grounds that it makes it easy to correct the system if prior records loaded are found to be in error, and records representing five minutes of time are cheap to process.) |
#7
| |||
| |||
|
|
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 |
#8
| |||
| |||
|
|
In article <hnkenq$2gm$1 (AT) news (DOT) eternal-september.org>, Cat 22 cat22 (AT) invalid (DOT) org> 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. Why separate fields, instead of a single DATETIME field holding both? 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. Create a uniqueness constraint on that table, referencing the date & time column(s). Then use INSERT IGNORE instead of INSERT. |
#9
| |||
| |||
|
|
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 |

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