Raised This Month: $ Target: $400
 0% 

MySQL Best way to prevent duplicating records?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 12-03-2013 , 13:29   MySQL Best way to prevent duplicating records?
Reply With Quote #1

Hey,

what's the best way to prevent duplicate insert mysql record in SourcePawn? In PHP I would use ON DUPLICATE KEY UPDATE, however, it won't work in SP (yes, I did try). Then I'm left with selecting the record first and if so include it? It's twicing the code, maybe there's any better way?
LambdaLambda is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 12-03-2013 , 13:39   Re: MySQL Best way to prevent duplicating records?
Reply With Quote #2

Define a primary key and use "INSERT OR UPDATE"?

Also are we talking SQL or SQLite (doublecheck)?
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.

Last edited by Dr. Greg House; 12-03-2013 at 13:40.
Dr. Greg House is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 12-04-2013 , 15:31   Re: MySQL Best way to prevent duplicating records?
Reply With Quote #3

I use "ON DUPLICATE KEY UPDATE" in one of my plugins. Don't remember it not working.
__________________
11530 is offline
-Absolute-
Member
Join Date: Mar 2010
Old 12-04-2013 , 15:48   Re: MySQL Best way to prevent duplicating records?
Reply With Quote #4

There is no difference between using mysql from php or source pawn. Make sure your tables are set up properly (having unique keys) or else it won't fire ON DUPLICATE
__________________
-Absolute- is offline
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 12-04-2013 , 15:57   Re: MySQL Best way to prevent duplicating records?
Reply With Quote #5

K i get it.

Sry fot not answearing, I really couldn't. I'll take a look at it later on.
LambdaLambda is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 12-05-2013 , 23:52   Re: MySQL Best way to prevent duplicating records?
Reply With Quote #6

I just tested and it works.
Code:
FormatEx(query, 256, "INSERT INTO playerpoints (auth, name, rank) VALUES ('%s', '%s', %d) ON DUPLICATE KEY UPDATE name = name;", auth, safeName, gI_Cache_ClientRank[client]);
No errors and no duplications.
__________________
retired
shavit is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 12-06-2013 , 07:44   Re: MySQL Best way to prevent duplicating records?
Reply With Quote #7

Or you could query, check the result rows. If there are, update, else insert...
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
-Absolute-
Member
Join Date: Mar 2010
Old 12-06-2013 , 08:03   Re: MySQL Best way to prevent duplicating records?
Reply With Quote #8

Quote:
Originally Posted by shavit View Post
I just tested and it works.
Code:
FormatEx(query, 256, "INSERT INTO playerpoints (auth, name, rank) VALUES ('%s', '%s', %d) ON DUPLICATE KEY UPDATE name = name;", auth, safeName, gI_Cache_ClientRank[client]);
No errors and no duplications.
This only works if your table's auth row is unique. Else it will just append since there was no duplicate.
__________________
-Absolute- is offline
Reply



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:09.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode