AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SQL]Only insert if not already exists (https://forums.alliedmods.net/showthread.php?t=126393)

NiQu 05-08-2010 23:56

[SQL]Only insert if not already exists
 
Hi, im making a plugin with the SQLx module and it connects fine.

But, im getting duplicate entries, i made it so it adds 1 entry with the server ip in the plugin_init, but everytime i restart or do a map change a duplicate entry is created in the database.

How do i fix that?

Current Code:
PHP Code:

    g_SqlTuple SQL_MakeDbTuple("secrethost""root""secretpass""mytable");
    
    new 
ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511);
    if(
SqlConnection == Empty_Handle)
    {
        
set_fail_state(g_Error);
    }
    
    new 
szIP[22], szPort[6];
    
get_cvar_string("ip"szIP15);
    
get_cvar_string("port"szPort5);
    
format(szIP21"%s:%s"szIPszPort);
    
    new 
Handle:Queries[1];
    new 
query[128];
    
formatex(querysizeof(query) - 1"INSERT IGNORE INTO plugin (server_ip) VALUES ('%s')"szIP);
    
    
Queries[0] = SQL_PrepareQuery(SqlConnectionquery);
        
    for(new 
Count;Count 3;Count++)
    {
        if(!
SQL_Execute(Queries[Count]))
        {
            
SQL_QueryError(Queries[Count],g_Error,511);
            
set_fail_state(g_Error);
        }
        
SQL_FreeHandle(Queries[Count]);
    }
    
SQL_FreeHandle(SqlConnection

Im using MySQL.

To make it clear: I dont want to insert the IP if the IP already exists in the DB.

01101101 05-09-2010 00:05

Re: [SQL]Only insert if not already exists
 
Make IP field an unique key.

NiQu 05-09-2010 00:07

Re: [SQL]Only insert if not already exists
 
Done, thanks it works ..

Another Q though, it gives me index out of bounds warning at line 125.

Line 125:
PHP Code:

if(!SQL_Execute(Queries[Count])) 

And i dont know how to fix that out, i guess it has something to do with the loop.

EDIT: ip_field->Primary Key works.


All times are GMT -4. The time now is 03:43.

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