Raised This Month: $ Target: $400
 0% 

Need mysql base


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 06-09-2010 , 17:41   Re: Need mysql base
Reply With Quote #13

Using prepare and execute query is not a good way to use sqlx.

This is how you do it:
PHP Code:
#include <amxmodx>
#include <sqlx>

new Handle:g_SqlTuple
new const g_sqlTable[] = "test_table"


public plugin_init(){
    
register_plugin("sqlite test""1.0""Sylwester")
    
sql_init()
    
register_clcmd("table_create""check_table")
    
register_clcmd("table_save""table_save")
    
register_clcmd("table_load""table_load")
    
register_clcmd("table_loadx""table_loadx")
}


public 
sql_init(){
    
SQL_SetAffinity("sqlite")
    
g_SqlTuple SQL_MakeDbTuple("127.0.0.1","root","","test_db")
}


public 
check_table(){
    new 
cache[512]
    
formatex(cache511"CREATE TABLE IF NOT EXISTS %s ( name VARCHAR( 64 ), ip VARCHAR( 64 ),authid VARCHAR( 64 ), class integer( 2 ) , lvl integer( 3 ) DEFAULT 1, exp integer( 9 ) DEFAULT 0, str integer( 3 ) DEFAULT 0, int integer( 3 ) DEFAULT 0, dex integer( 3 ) DEFAULT 0, agi integer( 3 ) DEFAULT 0, PRIMARY KEY ( `authid`,`class`) );",g_sqlTable)
    
log_amx("create table query: %s"cache)
    
SQL_ThreadQuery(g_SqlTuple"handle_create_table"cache)
    return 
PLUGIN_HANDLED
}


public 
handle_create_table(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        
log_amx("table %s creation failed"g_sqlTable)
        return 
PLUGIN_HANDLED
    
}

    
log_amx("table %s created..."g_sqlTable)
    return 
PLUGIN_HANDLED
}


public 
table_save(id){
    new 
args[32]
    
read_args(args31)
    
remove_quotes(args)
    new class = 
str_to_num(args)

    new 
cache[512]
    new 
steamid[32], ip[32], name[32]
    
get_user_name(idname31)
    
get_user_authid(idsteamid31)
    
get_user_ip(idip31)
    
formatex(cache511,"REPLACE INTO `%s` ( `authid` , `class` ,`name` , `ip` , `lvl` , `exp`, `str` , `int` , `dex` , `agi` ) VALUES ( '%s', '%d', '%s', '%s', '%d', '1337', '%d', '2', '3', '%d' );"g_sqlTablesteamid, class, nameip, class*10+1, class, random(100))
    
log_amx("save query: %s"cache)
    
SQL_ThreadQuery(g_SqlTuple"handle_save"cache)
    return 
PLUGIN_HANDLED
}


public 
handle_save(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    return 
PLUGIN_HANDLED
}


public 
table_load(id){
    new 
args[32]
    
read_args(args31)
    
remove_quotes(args)
    new class = 
str_to_num(args)    

    new 
cache[512]
    new 
steamid[32]
    
get_user_authid(idsteamid31)
    
formatex(cache511,"SELECT * FROM %s WHERE ( authid='%s' AND class='%d' );"g_sqlTablesteamid, class)
    
log_amx("load query: %s"cache)
    
SQL_ThreadQuery(g_SqlTuple"handle_load"cache)
    return 
PLUGIN_HANDLED

}


public 
handle_load(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    
    if(
SQL_MoreResults(Query)){
        new 
name[32], steamid[32], ip[32]
        new 
lvl SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"lvl"))
        new 
xp SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"exp"))
        
        new 
int SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"int"))
        new 
str SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"str"))
        new 
agi SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"agi"))
        new 
dex SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"dex"))
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"name"), name31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"ip"), ip31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"authid"), steamid31)
        
log_amx("retrieved result for %s %s %s, lvl %d xp %d, int %d, str %d, agi %d, dex %d"namesteamidiplvlxpintstragidex)

    }else{
        
log_amx("retrieved no results ....")
    }

    return 
PLUGIN_HANDLED
}


public 
table_loadx(id){
    new 
args[32]
    
read_args(args31)
    
remove_quotes(args)

    new 
cache[512]
    new 
steamid[32]
    
get_user_authid(idsteamid31)
    
formatex(cache511,"SELECT * FROM %s WHERE authid='%s';"g_sqlTablesteamid)
    
log_amx("load query: %s"cache)
    
SQL_ThreadQuery(g_SqlTuple"handle_loadx"cache)
    return 
PLUGIN_HANDLED

}


public 
handle_loadx(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    
    if(!
SQL_MoreResults(Query)){
        
log_amx("retrieved no results ....")
        return 
PLUGIN_HANDLED
    
}

    do{
        new 
name[32], steamid[32], ip[32]
        new class = 
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"class"))
        new 
lvl SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"lvl"))
        new 
xp SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"exp"))
        
        new 
int SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"int"))
        new 
str SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"str"))
        new 
agi SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"agi"))
        new 
dex SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"dex"))
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"name"), name31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"ip"), ip31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"authid"), steamid31)
        
log_amx("got res for %s %s %s, class %d, lvl %d, xp %d, int %d, str %d, agi %d, dex %d"namesteamidip, class, lvlxpintstragidex)
        
SQL_NextRow(Query)
    }while(
SQL_MoreResults(Query))

    return 
PLUGIN_HANDLED

You don't need mysql server to test this. You only need sqlite in your modules.ini
table_load/save has 1 argument - number of class
__________________
Impossible is Nothing
Sylwester is offline
 



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 05:20.


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