AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   if sql table ENTERY exist olny Update else insert - how to do this (https://forums.alliedmods.net/showthread.php?t=55282)

schuballaa 05-18-2007 11:53

if sql table ENTERY exist olny Update else insert - how to do this
 
Hello,
i get here very usefull help.

Now i get a new Problem. Frist i wanna say that i am a
beginner in this scripting language.

Problem:
now the server olny update the enterys if the server is already in the Table (i added it manual) (See picture 1).
The Updateing function works perfecly thanks pRED* | NZ he helpt me very very mutch.

http://www.schubinator.de/scriptingh...rtproblem1.jpg

Now i olny need a if function and here is the problem.
I tried many thinks but i get everytime the error must be indexed or someting else.

Maybe somebody knows how this if funktion have to look like.

here is my idear, but i am unable to comlile it...(compile errors)
I am sure thats there exist a easyer way to make ti work.

My try: (as ID i use the Server IP because its never Change):

Code:

public write() {
 new Server[200];
 get_cvar_string("net_address",Server,200);
 new Sql:mysql = dbi_connect("******", "******+", "********", "zbk")
 //Do a select query
 new Result:res = dbi_query(mysql,"SELECT Server FROM zbkcommunity WHERE Server=^"%s^"",Server")
 //If the query is less than or equal to RESULT_FAILED, you got an invalid result and can't do anything with it.
 if (res <= RESULT_FAILED)
 {
 new err[255]
 new errNum = dbi_error(mysql, err, 254)
 server_print("error3: %s|%d", err, errNum)
 return 1
 }
 //Loop through the result set
 if (res && dbi_nextrow(res)>0)
 {
 new exist[32]
 //Get the column/field called "keyname" from the result set
 dbi_result(res, "Server", exist, 31)
 // Now i have the Server saved in exist
 //Now i try to do a if command but this isnt work, dont know why, tried it many times
 new myserverip[200];
 get_cvar_string("net_address",myserverip,200);
 if (myserverip = exist) {
 //the server entery already exist...now i olny need to update it.
 dbi_query(dbc, "UPDATE zbkcommunity SET Spieler=%i, MaximaleSpieler=%i WHERE Server=^"%s^"",Spieler,MaximaleSpieler,Server)
 server_print("[AMXX] Ur Server already exist  - i update ur Informations")
 else
 // not this server found adding me in Table,
 dbi_query(dbc, "Insert INTO zbkcommunity VALUES ('%s', %i, %i)",Server,Spieler,MaximaleSpieler)
 server_print("[AMXX] Ur Server doesn't exist - Your server was added, Welcome")
 return PLUGIN_CONTINUE;
 }

I hope u can help me to make this working

If a new server is installing my plugin, its automatical should added in my sql Database, is this done it should update his informations every 15 seconds

pRED* 05-20-2007 00:58

Re: if sql table ENTERY exist olny Update else insert - how to do this
 
I've never done DBI (only SQLx) so I'm not entirely sure but..

When you run the UPDATE query it should return an error code. There will be a specific code that only happens when the query failed but the connection worked (eg That line in the table doesn't exist).

http://www.amxmodx.org/funcwiki.php?go=func&id=210
Using that you should be able to get a return value from dbi_query so you want something like this (might need testing, it's just a guess)

Code:
new return = dbi_query(dbc, "UPDATE zbkcommunity SET Spieler=%i, MaximaleSpieler=%i WHERE Server=^"%s^"",Spieler,MaximaleSpieler,Server) if (num<0) //If num is less than 0 (failed)) {     dbi_query(dbc, "Insert INTO zbkcommunity VALUES ('%s', %i, %i)",Server,Spieler,MaximaleSpieler) }

If that doesn't work try if num==0 instead

schuballaa 05-20-2007 22:16

Re: if sql table ENTERY exist olny Update else insert - how to do this
 
Thanks, i will try it today or tomorow (work) i will give feedback if it works, thank u 4 ur help anyway very very mutch


All times are GMT -4. The time now is 10:42.

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