Raised This Month: $ Target: $400
 0% 

[SQL]Only insert if not already exists


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
NiQu
Veteran Member
Join Date: Nov 2009
Old 05-08-2010 , 23:56   [SQL]Only insert if not already exists
Reply With Quote #1

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.
__________________
My Projects
  • RoTAPI V0.0.1 ------- Private
    • Progress - [||||||||||]
  • CashMod V0.0.6 ----- Public
    • Progress - [||||||||||]
  • CashMod V0.0.7 ----- Public
    • Progress - [||||||||||]
NiQu is offline
01101101
BANNED
Join Date: Nov 2009
Location: 9`su 09`n0n7e`r0f76a
Old 05-09-2010 , 00:05   Re: [SQL]Only insert if not already exists
Reply With Quote #2

Make IP field an unique key.
01101101 is offline
NiQu
Veteran Member
Join Date: Nov 2009
Old 05-09-2010 , 00:07   Re: [SQL]Only insert if not already exists
Reply With Quote #3

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.
__________________
My Projects
  • RoTAPI V0.0.1 ------- Private
    • Progress - [||||||||||]
  • CashMod V0.0.6 ----- Public
    • Progress - [||||||||||]
  • CashMod V0.0.7 ----- Public
    • Progress - [||||||||||]
NiQu 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 03:42.


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