View Single Post
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-28-2019 , 13:37   Re: SQL Insert and Update Player Info
Reply With Quote #7

You need to include a primary key in the statement otherwise it will work the same as insert. If any fields in the record have a value then you must include them in the statement, even if they are not being changed, otherwise they will be set to null. In the below example my primary key in the table is ID, set to auto-increment to make inserting new records easy.

Suppose I have the below record:

ID = 223
SteamID = STEAM_0:1:12345
PlayerName= bugsy
PlayTime= 555

I can then do the below to update PlayTime to 123. Fields SteamID & PlayerName will remain unchanged since I am including the existing values in the statement.

Code:
REPLACE INTO tblPlayerData (ID, SteamID, PlayerName, PlayTime) VALUES (223,'STEAM_0:0:12345','bugsy',123);
If I also have a column XPPoints in the table, it would be set to null with the above REPLACE INTO statement, unless I specify a value.

Assume these fields exist in a row:

ID = 223
SteamID = STEAM_0:1:12345
PlayerName= bugsy
PlayTime= 555
XPPoints = 555

Code:
REPLACE INTO tblPlayerData (ID, SteamID, PlayerName, PlayTime, XPPoints) VALUES (223,'STEAM_0:0:12345','bugsy',123,555);
This will retain XPPoints value of 555 (and leave SteamID & PlayerName as is), while changing PlayTime to 123.

When adding a new record, just exclude the ID field and it will add a new record, advancing ID to the next slot automatically.

Code:
REPLACE INTO tblPlayerData (SteamID, PlayerName) VALUES ('STEAM_0:0:555','bugsy2');
__________________

Last edited by Bugsy; 12-28-2019 at 14:21.
Bugsy is offline