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

Check if table exsits.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
erickvan
Junior Member
Join Date: Dec 2020
Old 05-29-2021 , 01:37   Check if table exsits.
Reply With Quote #1

Hello folks. Can someone do for me? I cant figure it out. I have code:

PHP Code:
public SavePlayerSession()
{
new 
g_maxplayers;
g_maxplayers get_maxplayers();
new 
p;
for(
1<= g_maxplayersp++)
{
if(
is_user_connected(p) && (<= get_user_team(p) <= 2))
{
new 
steam[64];
new 
szQuery[999];
new 
map_name[64];
get_mapname(map_name63)

get_user_authid(psteam63);
new 
szDirectory[128];
get_configsdirszDirectorycharsmaxszDirectory ) )
addszDirectorycharsmaxszDirectory ), "/id.txt" )

new 
size file_sizeszDirectory)  

new 
readMatchID[28], iLen

for ( new size i++ ) 

read_fileszDirectoryireadMatchIDcharsmaxreadMatchID ), iLen )
}
formatex(szQuery998"INSERT INTO `users_maps` (`steam_id`, `session_id`, `map` ) VALUES ('%s', '%s', '%s' );"steamreadMatchIDmap_name); 
SQL_ThreadQueryg_hTuple"QuerySetData"szQuery);
}
}
return 
PLUGIN_HANDLED;

Can someone make it: If steam_id and readMatchID exsits in the database just update not INSERT into? Thanks!

Last edited by erickvan; 05-29-2021 at 01:40.
erickvan is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-29-2021 , 23:12   Re: Check if table exsits.
Reply With Quote #2

In your CREATE TABLE script, use the below to define steam_id:
Code:
steam_id varchar(34) UNIQUE
and then to INSERT or UPDATE, use REPLACE INTO. This will create new if not exists or over-write the data for a given steam id if it exists.
Code:
REPLACE INTO users_maps (steam_id, session_id, map) VALUES ('%s', '%s', '%s' );", steam, readMatchID, map_name);
__________________
Bugsy is offline
erickvan
Junior Member
Join Date: Dec 2020
Old 05-30-2021 , 13:03   Re: Check if table exsits.
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
In your CREATE TABLE script, use the below to define steam_id:
Code:
steam_id varchar(34) UNIQUE
and then to INSERT or UPDATE, use REPLACE INTO. This will create new if not exists or over-write the data for a given steam id if it exists.
Code:
REPLACE INTO users_maps (steam_id, session_id, map) VALUES ('%s', '%s', '%s' );", steam, readMatchID, map_name);
Thanks i will try!
erickvan is offline
erickvan
Junior Member
Join Date: Dec 2020
Old 05-31-2021 , 02:23   Re: Check if table exsits.
Reply With Quote #4

Quote:
Originally Posted by Bugsy View Post
In your CREATE TABLE script, use the below to define steam_id:
Code:
steam_id varchar(34) UNIQUE
and then to INSERT or UPDATE, use REPLACE INTO. This will create new if not exists or over-write the data for a given steam id if it exists.
Code:
REPLACE INTO users_maps (steam_id, session_id, map) VALUES ('%s', '%s', '%s' );", steam, readMatchID, map_name);
Ok i need another idea. Why? Becouse its deleting all data. I need just update if exsits or if not exsits create new. Any ideas?
erickvan is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-31-2021 , 09:25   Re: Check if table exsits.
Reply With Quote #5

If you did what I told you to do correctly it would do exactly what you want.

Something like this:
Code:
CREATE TABLE IF NOT EXISTS users_maps (ID INTEGER PRIMARY KEY AUTOINCREMENT, steam_id VARCHAR(34) UNIQUE, session_id VARCHAR(12), map VARCHAR(32));
Then
Code:
REPLACE INTO users_maps (steam_id, session_id, map) VALUES ('%s', '%s', '%s' );", steam, readMatchID, map_name);
Calling this would insert a new record for STEAM_0:0:12345
REPLACE INTO users_maps (steam_id, session_id, map) VALUES ('STEAM_0:0:12345', 'testID1', 'map1" );

Calling this would over-write STEAM_0:0:12345's existing record with testID2 and map2 (technically delete the old and insert new)
REPLACE INTO users_maps (steam_id, session_id, map) VALUES ('STEAM_0:0:12345', 'testID2', 'map2" );
__________________

Last edited by Bugsy; 05-31-2021 at 11:00.
Bugsy 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 13:06.


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