AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SOLVED] SQLX problem... :( (https://forums.alliedmods.net/showthread.php?t=56433)

regalis 06-13-2007 16:30

[SOLVED] SQLX problem... :(
 
Hi guys, im new to SQL stuff and i didn't find the bug in my script...
I want to save some configurations from the players in a DB.

And i got this error:
Code:

L 06/13/2007 - 22:17:30: [AMXX] Run time error 10: native error (native "SQL_ReadResult")
L 06/13/2007 - 22:17:30: [AMXX]    [0] phphf0lD6.sma::ReadHandle (line 532)

after that i changed the line 532:
Code:

from: SQL_ReadResult(Query, 1, g_Flags[Data[0]], 9);
into: SQL_ReadResult(Query, 0, g_Flags[Data[0]], 9);

but it doesn't work..there are no flags in the DB :(

Now i get this error:
Code:

L 06/13/2007 - 22:26:34: [blabla.amxx] [CFG] Error on query: Duplicate entry 'STEAM_0:0:2569349' for key 1
L 06/13/2007 - 22:26:20: [AMXX] Run time error 10: native error (native "SQL_ThreadQuery")
L 06/13/2007 - 22:26:20: [AMXX]    [0] phphf0lD6.sma::SQL_init_Write (line 590)
L 06/13/2007 - 22:26:20: [AMXX]    [1] phphf0lD6.sma::write_client_init (line 578)
L 06/13/2007 - 22:26:20: [AMXX]    [2] phphf0lD6.sma::WriteHandle (line 566)

The problem was that i have used insert everytime, but if a steamid is already in the DB there comes an error about duplicate entry..so i have tried to update the DB, but i have no idea how to do this....
Can anyone please help me with that im going crazy :(

here is the DB code...
If you need more details please feel free to ask...thx :)
Code:

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <cstrike>
#include <fun>
#include <sqlx>


// DB Stuff
new gcfgHost;
new gcfgUser;
new gcfgPass;
new gcfgDatabase;
new Handle:g_cfg_SqlTuple;
new g_cfg_Cache[512];
new g_Flags[MAX_PLAYERS][10];


public plugin_init()
{
    // ConfigDB-Stuff
    gcfgHost = register_cvar("config_sql_host", "host", FCVAR_PROTECTED);
    gcfgUser = register_cvar("config_sql_user", "user", FCVAR_PROTECTED);
    gcfgPass = register_cvar("config_sql_pass", "pass", FCVAR_PROTECTED);
    gcfgDatabase = register_cvar("config_sql_db", "amx", FCVAR_PROTECTED);
}


public client_putinserver(id)
{
    //User config
    read_client_config(id);
}


public client_disconnect(id)
{
    write_client_config(id);
}


public plugin_end()
{
    SQL_FreeHandle(g_cfg_SqlTuple);
    SQL_FreeHandle(gSqlHandle);
}


read_client_config(id)
{
    new steamid[MAX_STR];
    get_user_authid(id, steamid, MAX_STR-1);
   
    format(g_cfg_Cache, 511, "SELECT * FROM userconfigs WHERE steamid='%s'", steamid);
    SQL_Query_Read(id, g_cfg_Cache);
}


SQL_Query_Read(id, query[], {Float, Sql, Result, _}:...)
{
    static data[1];
    static formattedQuery[MAX_QUERY];
   
    data[0] = id;
    formattedQuery[0] = 0;
   
    vformat(formattedQuery, MAX_QUERY-1, query, 2);
    SQL_ThreadQuery(g_cfg_SqlTuple, "ReadHandle", formattedQuery, data, 1);
}


public ReadHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(FailState == TQUERY_CONNECT_FAILED) log_amx("[EA-CFG] Could not connect to SQL database.");
    else if(FailState == TQUERY_QUERY_FAILED) log_amx("[EA-CFG] Query failed.");
    if(Errcode)
    {
        write_client_config(Data[0]);
        return log_amx("[EA-CFG] Error on query: %s",Error);
    }
    else
    {
        SQL_ReadResult(Query, 0, g_Flags[Data[0]], 9);
        client_print(0, print_chat, "zomg, some data-flags: %s", g_Flags[Data[0]]);
        SetConfigFlags(Data[0]);
    }
    return PLUGIN_CONTINUE;
}

write_client_config(id)
{
    new steamid[MAX_STR];
    get_user_authid(id, steamid, MAX_STR-1);
       
    format(g_cfg_Cache, 511, "INSERT INTO userconfigs VALUES('%s','%s')", steamid, g_Flags[id]);
    SQL_Query_Write(id, g_cfg_Cache);
}

SQL_Query_Write(id, query[], {Float, Sql, Result, _}:...)
{
    static data[1];
    static formattedQuery[MAX_QUERY];
   
    data[0] = id;
    formattedQuery[0] = 0;
   
    vformat(formattedQuery, MAX_QUERY-1, query, 2);
    SQL_ThreadQuery(g_cfg_SqlTuple, "WriteHandle", formattedQuery, data, 1);
}

public WriteHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(FailState == TQUERY_CONNECT_FAILED) log_amx("[EA-CFG] Could not connect to SQL database.");
    else if(FailState == TQUERY_QUERY_FAILED) log_amx("[EA-CFG] Query failed.");
    if(Errcode)
    {
        write_client_init(Data[0]);
        return log_amx("[EA-CFG] Error on query: %s",Error);
    }
    return PLUGIN_CONTINUE;
}

write_client_init(id)
{
    new steamid[MAX_STR];
    get_user_authid(id, steamid, MAX_STR-1);
       
    format(g_cfg_Cache, 511, "UPDATE userconfigs SET flags = '%s' WHERE steamid = '%s'", g_Flags[id], steamid);
    SQL_init_Write(id, g_cfg_Cache);
}

SQL_init_Write(id, query[], {Float, Sql, Result, _}:...)
{
    static data[1];
    static formattedQuery[MAX_QUERY];
   
    data[0] = id;
    formattedQuery[0] = 0;
   
    vformat(formattedQuery, MAX_QUERY-1, query, 2);
    SQL_ThreadQuery(g_cfg_SqlTuple, "InitHandle", formattedQuery, data, 1);
}

public InitHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
    if(FailState == TQUERY_CONNECT_FAILED) log_amx("[EA-CFG]Could not connect to SQL Config-Database.");
    else if(FailState == TQUERY_QUERY_FAILED) log_amx("[EA-CFG] Query failed.");
   
    if(Errcode)
    {
        write_client_init(Data[0]);
        return log_amx("[EA-CFG] Error on query: %s",Error);
    }
    return PLUGIN_CONTINUE;
}










addToFlags(id, Flag[])
{
    if(contain(g_Flags[id], Flag))
    {
        //log_amx("Error: Flag already exist in PlayerFlagString!");
        return;
    }
    if(equal(Flag, "Y"))
    {
        removeFromFlags(id, "G");
        removeFromFlags(id, "T");
    }
    if(equal(Flag, "G"))
    {
        removeFromFlags(id, "Y");
        removeFromFlags(id, "T");
    }
    if(equal(Flag, "T"))
    {
        removeFromFlags(id, "Y");
        removeFromFlags(id, "G");
    }
    add(g_Flags[id], 9, Flag);   
}

removeFromFlags(id, Flag[])
{
    if(contain(g_Flags[id], Flag))
    {
        replace_all(g_Flags[id], 9, Flag, "");
    }
}

SetConfigFlags(id)
{
    // TimeBar
    if(contain(g_Flags[id], "B"))
    {
        g_ShowTimeBar[id] = false;
        if(task_exists(id+110477)) remove_task(id+110477);
    }
    else
    {
        g_ShowTimeBar[id] = true;
        set_task(1.0, "timershow", id+110477, "", 0, "b");
    }
    // ReloadBar
    if(contain(g_Flags[id], "R"))
    {
        g_ShowReloadBar[id] = false;
    }
    else
    {
        g_ShowReloadBar[id] = true;
    }
    // Join/Leave
    if(contain(g_Flags[id], "J"))
    {
        g_ShowJoinLeave[id] = false;
    }
    else
    {
        g_ShowJoinLeave[id] = true;
    }
    // ChatColors
    if(contain(g_Flags[id], "Y"))    g_chatcolor[id] = 1;
    if(contain(g_Flags[id], "G")) g_chatcolor[id] = 2;
    if(contain(g_Flags[id], "T")) g_chatcolor[id] = 3;
}



greetz regalis

pRED* 06-13-2007 23:04

Re: SQLX problem... :(
 
I havn't read all the code but.. On bf2 mod I..

Client connect - attempt to load data (like u've already got). If the query succeeds but has no data (no current entry for that user) then do an insert of all 0's (or '' if it's a string).

Then when you try save always use an update query because everyone will have an entry in the table

teame06 06-14-2007 00:40

Re: SQLX problem... :(
 
http://dev.mysql.com/doc/refman/4.1/...duplicate.html

Use this type of query. The problem is that your database already has the player info and insert can't update the row by itself.

You can use REPLACE ALSO instead of the statement above.

regalis 06-14-2007 05:28

Re: SQLX problem... :(
 
Quote:

Originally Posted by pRED* | NZ (Post 489428)
I havn't read all the code but.. On bf2 mod I..

Client connect - attempt to load data (like u've already got). If the query succeeds but has no data (no current entry for that user) then do an insert of all 0's (or '' if it's a string).

Then when you try save always use an update query because everyone will have an entry in the table

Thank you for your answer :)
I don't know how to check if the Query retrieve Data...0o
But i think i got it working with teame06's solution.

Quote:

Originally Posted by teame06 (Post 489452)
http://dev.mysql.com/doc/refman/4.1/...duplicate.html

Use this type of query. The problem is that your database already has the player info and insert can't update the row by itself.

You can use REPLACE ALSO instead of the statement above.

That's pretty nice with "ON DUPLICATE KEY UPDATE" wonder since when this is included to mysql..anyway now it seems to work *smile*

Thank you all for your interest in my problem! :up:

greetz regalis

pRED* 06-14-2007 19:16

Re: [SOLVED] SQLX problem... :(
 
Just for future reference...

This is what I have inside my select query handler (after the error checking)

Code:
    if (!SQL_MoreResults(Query)) // No more results - User not found, create them a blank entry in the table. and zero their variables     {         for (new counter=0; counter<6; counter++)         {               g_PlayerBadges[id][counter] = 0 ;                     }         knifekills[id]=0         pistolkills[id]=0         sniperkills[id]=0         parakills[id]=0         totalkills[id]=0         defuses[id]=0         plants[id]=0         explosions[id]=0         format(g_Cache,511,"INSERT INTO bf2ranks VALUES('%s','0','0','0','0','0','0','0','0','0','0','0','0','0','0')",authid[id]);         SQL_ThreadQuery(g_SqlTuple,"QueryHandle",g_Cache)         return PLUGIN_CONTINUE;     }     //Player must have been found. Loop through and load the columns into the global vars     for (new counter=0; counter<6; counter++)     {           g_PlayerBadges[id][counter] = SQL_ReadResult(Query,counter)                   }     knifekills[id]=SQL_ReadResult(Query,6)     pistolkills[id]=SQL_ReadResult(Query,7)     sniperkills[id]=SQL_ReadResult(Query,8)     parakills[id]=SQL_ReadResult(Query,9)     totalkills[id]=SQL_ReadResult(Query,10)     defuses[id]=SQL_ReadResult(Query,11)     plants[id]=SQL_ReadResult(Query,12)     explosions[id]=SQL_ReadResult(Query,13)


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

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