Raised This Month: $51 Target: $400
 12% 

Solved mysql duplicate


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
lexzor
Veteran Member
Join Date: Nov 2020
Old 12-25-2021 , 00:38   mysql duplicate
Reply With Quote #1

hello, first of all i wish you a merry christmas !

i have a question.

how should i use primary key when i create a table and update it.

i mean i have this table

PHP Code:
new const g_szTablesInfo[][] =
{
    
"(`hostname` VARCHAR(55) NOT NULL ,\
    `ipaddress` VARCHAR(22) NOT NULL ,\
    `onlineplayers` INT(3) NOT NULL ,\
    `onlineadmins` INT(3) NOT NULL ,\
    `currentmap` VARCHAR(55) NOT NULL ,\
    `nextmap` VARCHAR(55) NOT NULL ,\
    `maxplayers` INT(2) NOT NULL ,\
    `hide` INT(2) NOT NULL ,\
    PRIMARY KEY (`hostname`))"
}; 
and this is how i update it


PHP Code:
if(iResults == 0)
    {    
        
formatex(szQuerycharsmax(szQuery), "INSERT INTO `%s` (`hostname`,`ipaddress`,`onlineplayers`,`onlineadmins`,`currentmap`,`nextmap`,`maxplayers`,`hide`) VALUES ('%s','%s','%i','%i','%s','%s','%i','0')",
        
g_szTables[0], g_ServerData[HostName], g_ServerData[InternetAddress], g_ServerData[OnlinePlayers], g_ServerData[OnlineAdmins], g_ServerData[CurrentMap], g_ServerData[NextMap], g_ServerData[MaxPlayers]);        
        
SQL_ThreadQuery(g_SqlTuple"FreeHandle"szQuery);
    }
    else
    {
        
formatex(szQuerycharsmax(szQuery), "UPDATE `%s` SET `hostname` = '%s' , `ipaddress` = '%s' , `onlineplayers` = '%i' , `onlineadmins` = '%i' , `currentmap` = '%s' , `nextmap` = '%s' , `maxplayers` = '%i'",
        
g_szTables[0], g_ServerData[HostName], g_ServerData[InternetAddress], g_ServerData[OnlinePlayers], g_ServerData[OnlineAdmins], g_ServerData[CurrentMap], g_ServerData[NextMap], g_ServerData[MaxPlayers]);
        
SQL_ThreadQuery(g_SqlTuple"FreeHandle"szQuery);
    } 
but i get this error.

Code:
SQL ERROR: Duplicate entry 'server name' for key 'PRIMARY'

Last edited by lexzor; 12-27-2021 at 03:02.
lexzor is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 12-25-2021 , 03:45   Re: mysql duplicate
Reply With Quote #2

You have two same entries in your table for a Primary key.
A MySQL Primary key can't be in duplicate because it is unique. Delete one entry from your table and it should work.
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 12-25-2021 , 06:25   Re: mysql duplicate
Reply With Quote #3

but why the plugin doesn t update that row?

PHP Code:
public update_server_info()
{
    
get_server_info();
    new 
szQuery[512];
    
formatex(szQuerycharsmax(szQuery), "SELECT * FROM `%s` WHERE `ipaddress` = '%s'"g_szTables[0], g_ServerData[InternetAddress]);
    
SQL_ThreadQuery(g_SqlTuple"UpdateServerInfo"szQuery);
}

public 
UpdateServerInfo(FailStateHandle:QueryszError[], ErrorCodeszData[], iSize)
{
    if(
FailState || ErrorCode)
        
server_print("^n[%s] SQL ERROR: %s^n"g_szPluginNameszError);
        
    new 
szQuery[512];    

    new 
iResults SQL_NumResults(Query);

    if(
iResults == 0)
    {    
        
formatex(szQuerycharsmax(szQuery), "INSERT INTO `%s` (`hostname`,`ipaddress`,`onlineplayers`,`onlineadmins`,`currentmap`,`nextmap`,`maxplayers`,`hide`) VALUES ('%s','%s','%i','%i','%s','%s','%i','0')",
        
g_szTables[0], g_ServerData[HostName], g_ServerData[InternetAddress], g_ServerData[OnlinePlayers], g_ServerData[OnlineAdmins], g_ServerData[CurrentMap], g_ServerData[NextMap], g_ServerData[MaxPlayers]);        
        
SQL_ThreadQuery(g_SqlTuple"FreeHandle"szQuery);
    }
    else
    {
        
formatex(szQuerycharsmax(szQuery), "UPDATE `%s` SET `hostname` = '%s' , `ipaddress` = '%s' , `onlineplayers` = '%i' , `onlineadmins` = '%i' , `currentmap` = '%s' , `nextmap` = '%s' , `maxplayers` = '%i'",
        
g_szTables[0], g_ServerData[HostName], g_ServerData[InternetAddress], g_ServerData[OnlinePlayers], g_ServerData[OnlineAdmins], g_ServerData[CurrentMap], g_ServerData[NextMap], g_ServerData[MaxPlayers]);
        
SQL_ThreadQuery(g_SqlTuple"FreeHandle"szQuery);
    }
    

i just want to add it if doesn t exists and update some fields if exists.
lexzor is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 12-25-2021 , 07:28   Re: mysql duplicate
Reply With Quote #4

Quote:
Originally Posted by lexzor View Post
but why the plugin doesn t update that row?
Because mysql throws an error ( explained above ), as written in your script:

if(FailState || ErrorCode)
server_print("^n[%s] SQL ERROR: %s^n", g_szPluginName, szError);
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 12-25-2021 , 16:14   Re: mysql duplicate
Reply With Quote #5

You are updating the hostname therefore you must follow this example
Code:
formatex(szQuery, charsmax(szQuery), "UPDATE `%s` SET `ipaddress` = '%s' WHERE `hostname` = '%s';", str1, str2);

Last edited by bigdaddy424; 12-25-2021 at 16:15. Reason: hostname is set as the primary key
bigdaddy424 is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 12-27-2021 , 03:02   Re: mysql duplicate
Reply With Quote #6

Solved, thanks
lexzor is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 12-27-2021 , 05:52   Re: mysql duplicate
Reply With Quote #7

Why on failstate you keep executing code, just stop and add return, its really annoying to see alot of coders do this mistake.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
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 18:01.


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