| Sylwester |
05-28-2010 04:55 |
Re: sqlx example?
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(cache, 511, "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)", Error, Errcode) return PLUGIN_HANDLED } new cache[512] log_amx("check_table success, starting to read results...") while(SQL_MoreResults(Query)){ SQL_ReadResult(Query, 0, cache, 511) log_amx("got res: %s", cache) if(equal(cache, g_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(cache, 511, "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)", Error, Errcode) 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(args, 31) remove_quotes(args) new klasa = str_to_num(args)
new cache[512] new steamid[32], ip[32], name[32] get_user_name(id, name, 31) get_user_authid(id, steamid, 31) get_user_ip(id, ip, 31) formatex(cache, 511,"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_sqlTable, steamid, klasa, name, ip, klasa*10+1, klasa, 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)", Error, Errcode) return PLUGIN_HANDLED }
return PLUGIN_CONTINUE }
public table_load(id){ new args[32] read_args(args, 31) remove_quotes(args) new klasa = str_to_num(args)
new cache[512] new steamid[32] get_user_authid(id, steamid, 31) formatex(cache, 511,"SELECT * FROM %s WHERE ( sid='%s' AND klasa='%d' );", g_sqlTable, steamid, klasa) 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)", Error, Errcode) 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"), name, 31) SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"ip"), ip, 31) SQL_ReadResult(Query,SQL_FieldNameToNum(Query,"sid"), steamid, 31) log_amx("got res for %s %s %s, lvl %d xp %d, int %d, str %d, agi %d, dex %d", name, steamid, ip, lvl, xp, int, str, agi, dex)
}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"
|