SQLX
I started to switch from DBI to SQLX but already got lost, I went threw Hawk's tutorial and any plugins that were using SQLX. But i'm still stumpt on a few things. Here's my code in DBI:
Code:
#include <amxmodx>
#include <dbi>
#define PLUGIN "SQL Test"
#define VERSION "1.0"
#define AUTHOR "desu"
new Sql:dbc
new Result:result
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
set_task(0.5,"init_sql")
}
public init_sql()
{
new host[64], user[33], pass[32], db[32], error[32]
get_cvar_string("amx_sql_host", host, 63)
get_cvar_string("amx_sql_user", user, 63)
get_cvar_string("amx_sql_pass", pass, 63)
get_cvar_string("amx_sql_db", db, 63)
dbc = dbi_connect(host,user,pass,db,error,32)
if (dbc == SQL_FAILED)
{
server_print("[AMXX] Unable to connect to SQL")
return
}
}
public client_putinserver(id)
{
if(is_user_database(id)) {
console_print(0,"[AMXX SQL] Your steamid is registered")
}
else
console_print(0,"[AMXX SQL] Your steamid was not found")
}
// Does the person have a registed SteamID?
public is_user_database(id)
{
if(dbc < SQL_OK) return 0
new authid[32], query[256]
get_user_authid(id,authid,31)
format(query,255,"SELECT steamid FROM main WHERE steamid='%s'",authid)
result = dbi_query(dbc,"%s",query)
if(dbi_nextrow(result) > 0)
{
dbi_free_result(result)
return 1
} else {
dbi_free_result(result)
}
return 0
}
Here it is in SqlX:
Code:
#include <amxmodx>
#include <sqlx>
#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "desu"
// SQL Settings
new Handle:g_SqlX
new Handle:g_SqlConnection
new g_error[512]
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
set_task(0.5,"init_sql")
}
public init_sql()
{
new host[64], user[64], pass[64], db[64],error,errorcode
get_cvar_string("amx_sql_host", host, 63)
get_cvar_string("amx_sql_user", user, 63)
get_cvar_string("amx_sql_pass", pass, 63)
get_cvar_string("amx_sql_db", db, 63)
g_SqlX = SQL_MakeDbTuple(host, user, pass, db)
g_SqlConnection = SQL_Connect(g_SqlX,errorcode,g_error,511);
if (!g_SqlConnection) {
return
}
console_print(0,"[AMXX SQL] Connected!")
}
public client_putinserver(id)
{
if(is_user_database(id)) {
console_print(0,"[AMXX SQL] Your steamid is registered")
}
else
console_print(0,"[AMXX SQL] Your steamid was not found")
}
// Does the person have a registed SteamID?
public is_user_database(id)
{
// How can i check if the connection is established?
SQL_ThreadQuery(g_SqlX,"SelectHandle","SELECT steamid FROM main")
//if blah blah - How do I check to see if it returns any data?
}
//SQL Handles (Thanks Hawk)
public SelectHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
if(FailState == TQUERY_CONNECT_FAILED)
return set_fail_state("Could not connect to SQL database.")
else if(FailState == TQUERY_QUERY_FAILED)
return set_fail_state("Query failed.")
if(Errcode)
return log_amx("Error on query: %s",Error)
new DataNum
while(SQL_MoreResults(Query))
{
DataNum = SQL_ReadResult(Query,0)
server_print("zomg, some data: %s",DataNum)
SQL_NextRow(Query)
}
return PLUGIN_CONTINUE
}
I can't figure out how to know when it returns data to check to see if there steamid is in the table. (I know the handle returns data, but how do I grab it from another function?) I also have no idea what the "SQL_PrepareQuery" is used for, when you can just use "SQL_ThreadQuery"
|