caesar has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(jeffa) Re: simple MySQL prob
by jeffa (Bishop) on Mar 03, 2002 at 21:45 UTC | |
jeffa L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat) | [reply] |
|
Re: simple MySQL prob
by Tardis (Pilgrim) on Mar 03, 2002 at 23:28 UTC | |
Example:
Instance 1: Does key XXX exist? DB: Nope. Instance 2: Does key XXX exist? DB: Nope Instance 1: Please insert new row with key XXX DB: Done! Instance 2: Please insert new row with key XXX DB: <Explosion>Check the DBI manpage for details on transaction support. If you aren't using DBI, or your database doesn't support transactions, you should use one that does :-) Alternatively, code your database schema and application such that it doesn't need to do such a check. This may well be possible, but it's impossible to say without knowing your application. | [reply] |
|
Re: simple MySQL prob
by fuzzyping (Chaplain) on Mar 03, 2002 at 23:12 UTC | |
UPDATE: As Tardis alludes to below, you might need to worry about transactions, depending on your usage. I ass-u-me'd that you'd be performing this in a similar context to mine, where I run it in an isolated environment via cron. That may or may not be the case. -fuzzyping | [reply] [d/l] |
|
Re: simple MySQL prob
by screamingeagle (Curate) on Mar 03, 2002 at 21:44 UTC | |
fire an SQL query before the the INSERT statement to check whether the value exists : if the value does not exist, the VALEXISTS column will be 0. depending on this value, u can decide whether to fire the INSERT statement or not :-) | [reply] [d/l] |
|
Re: simple MySQL prob
by Zaxo (Archbishop) on Mar 03, 2002 at 23:21 UTC | |
MySQL INSERT will fail if you try to form a new record with a duplicate of a unique field. It's poor form to rely on error conditions for program logic, so the two step process of jeffa and screamingeagle is best. If you instead wish to only replace NULL values in an existing record, Note the distinction between INSERT and UPDATE, INSERT creates new records, UPDATE modifies existing ones. After Compline, | [reply] [d/l] |
|
Re: simple MySQL prob
by gav^ (Curate) on Mar 03, 2002 at 23:34 UTC | |
If you wanted to replace an existing record, look up the syntax for REPLACE where you can do something like: Hope this helps... gav^ | [reply] [d/l] [select] |
|
Re: simple MySQL prob
by webadept (Pilgrim) on Mar 04, 2002 at 02:37 UTC | |
Transactions are good if they are available and you want to deal with them. What I do is get session_id's working and a unique identifier, grabing an MD5 of the current time in seconds is normally pretty good. then build the table to keep user information seperate. ie
To keep things fast I always place indexs on the session_id and primary key combination. This will speed things up enough that using a hash won't increase recall speed much faster. With multiple users getting at your CGI this takes out the possibility that user 1, is adding duplicates to user 2 and so on. Also with the primary key, there is not a chance of a real duplicate record getting in there. Of course all of this is based on the idea that you are only worried about a single user putting in duplicate data such as items in a shopping cart or account information. If you have global informtion that is not of the "single user" type, say dictionary information or bills paid with mulitple data entry, then you are back to transactions. Man, did I get all talkitive or what? geez.. maybe just having "anyone" put stuff in here isn't all that good of an idea :-) Hope this helps, webadept.net | [reply] [d/l] |
|
Re: simple MySQL prob
by thor (Priest) on Mar 04, 2002 at 01:47 UTC | |
| [reply] |