AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Check if table exsits. (https://forums.alliedmods.net/showthread.php?t=332725)

erickvan 05-29-2021 01:37

Check if table exsits.
 
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!

Bugsy 05-29-2021 23:12

Re: Check if table exsits.
 
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);

erickvan 05-30-2021 13:03

Re: Check if table exsits.
 
Quote:

Originally Posted by Bugsy (Post 2748243)
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 05-31-2021 02:23

Re: Check if table exsits.
 
Quote:

Originally Posted by Bugsy (Post 2748243)
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?

Bugsy 05-31-2021 09:25

Re: Check if table exsits.
 
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" );


All times are GMT -4. The time now is 02:33.

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