Raised This Month: $ Target: $400
 0% 

sqlx example?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 05-28-2010 , 04:55   Re: sqlx example?
Reply With Quote #2

You should have posted more details about your problem (like what kind of variables do you need to save), because since you were unable to understand Hawks tut, then simple example probably will not help you.

Anyway here is example. I used this to learn how to use sqlite (you don't need sql server for this - it saves data in amxmodx\data\sqlite3\<db name>.sq3, to make it work with mysql you just need to use "create table if not exists" instead of requesting name from SQLite_Master):
PHP Code:
#include <amxmodx>
#include <sqlx>

new Handle:g_SqlTuple
new g_sqlTable[] = "test_table1"


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


public 
sql_init(){
    new 
host[128]
    new 
user[64]
    new 
pass[64]
    new 
database[64]
    new 
db_type[16]

    
get_cvar_string("amx_sql_type",db_type,15)
    
SQL_SetAffinity(db_type)
    
get_cvar_string("amx_sql_db",database,63)
    
get_cvar_string("amx_sql_host",host,127)
    
get_cvar_string("amx_sql_user",user,63)
    
get_cvar_string("amx_sql_pass",pass,63)

    
g_SqlTuple SQL_MakeDbTuple(host,user,pass,database)
}


public 
check_table(){
    new 
cache[512]
    
log_amx("sending check table query...")
    
formatex(cache511"SELECT name FROM SQLite_Master")
    
SQL_ThreadQuery(g_SqlTuple"handle_query"cache)
    return 
PLUGIN_HANDLED
}


public 
handle_query(FailState,Handle:Query,Error[],Errcode,Data[],DataSize){
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    new 
cache[512]
    
log_amx("check_table success, starting to read results...")
    while(
SQL_MoreResults(Query)){
        
SQL_ReadResult(Query0cache511)
        
log_amx("got res: %s"cache)
        if(
equal(cacheg_sqlTable)){
            
log_amx("table %s exists"g_sqlTable)
            return 
PLUGIN_HANDLED
        
}
        
SQL_NextRow(Query)
    }

    
log_amx("table %s does NOT exist, creating..."g_sqlTable)

    
formatex(cache511"CREATE TABLE %s ( nick VARCHAR( 64 ), ip VARCHAR( 64 ),sid VARCHAR( 64 ), klasa 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 ( `sid`,`klasa`) );",g_sqlTable)
    
log_amx("create table query: %s"cache)
    
SQL_ThreadQuery(g_SqlTuple"handle_create_table"cache)
    return 
PLUGIN_CONTINUE
}


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_CONTINUE
}


public 
table_save(id){
    new 
args[32]
    
read_args(args31)
    
remove_quotes(args)
    new 
klasa 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` ( `sid` , `klasa` ,`nick` , `ip` , `lvl` , `exp`, `str` , `int` , `dex` , `agi` ) VALUES ( '%s', '%d', '%s', '%s', '%d', '1337', '%d', '2', '3', '%d' );"g_sqlTablesteamidklasanameipklasa*10+1klasarandom(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_CONTINUE
}


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

    new 
cache[512]
    new 
steamid[32]
    
get_user_authid(idsteamid31)
    
formatex(cache511,"SELECT * FROM %s WHERE ( sid='%s' AND klasa='%d' );"g_sqlTablesteamidklasa)
    
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,"nick"), name31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"ip"), ip31)
        
SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"sid"), steamid31)
        
log_amx("got res 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_CONTINUE

amxmodx\configs\sql.cfg;
Code:
amx_sql_host    "127.0.0.1"
amx_sql_user    "root"
amx_sql_pass    ""
amx_sql_db    "amx"
amx_sql_table    "admins"
amx_sql_type    "sqlite"
__________________
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:28.


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