AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   SQLX (https://forums.alliedmods.net/showthread.php?t=51377)

Drak 02-16-2007 20:21

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"

YamiKaitou 02-16-2007 20:45

Re: SQLX
 
I know that the SQL_PrepareQuery returns a Hangle:query and you execute it by using SQL_Execute(Handle:query). Then I just use SQL_ReadResults(Handle:query,column[,extra stuff]) to get the results. But all of my querys are always only affecting one line, so I don't have to worry about changing rows.

To see if the connection is established, I just use if(g_DBCon){//stuff dealing with db}

Hawk552 02-16-2007 21:53

Re: SQLX
 
I don't think you understand how a threaded query works: it is asynchronous, meaning it does not actually "happen" the moment you execute it. This means you cannot extract the data from it in the same function it is executed.

With your code, you would have to do the checking in the "SelectHandle" function, or move it back to non-threaded querying. The native to retrieve the data is SQL_ReadResult (there's also a field one that I can't remember).

Drak 02-16-2007 22:17

Re: SQLX
 
Code:
public is_user_registered(id) {     if(!g_SqlConnection) { // We lost connection, don't continue.         return 0     }     new authid[33]     get_user_authid(id,authid,32)     //format(query,255,"SELECT steamid FROM main WHERE steamid='%s'",authid)     new Handle:Query = SQL_PrepareQuery(g_SqlConnection,"SELECT steamid FROM main WHERE steamid='%s'",authid)     if(!SQL_Execute(Query))     {         // if there were any problems         SQL_QueryError(Query,g_error,511)         set_fail_state(g_error)     }     // checks to make sure there's more results     // notice that it starts at the first row, rather than null     new Data     while(SQL_MoreResults(Query))     {         Data = SQL_ReadResult(Query,0)         SQL_NextRow(Query)         console_print(0,"[AMXX SQL] Your SteamID: %s Matched with the SQL SteamID: %s",authid,Query)         SQL_FreeHandle(Query)         return 1     }     return 0     //SQL_FreeHandle(g_SqlConnection) }
This seemed to work, but I still am clueless on some parts. Like, is it necessary to close the connection after the Query is complete?

Hawk552 02-16-2007 22:24

Re: SQLX
 
No, and your method is pretty stupid TBH, but I'm not going to argue.

You can just close the connection on plugin_end instead of then.


All times are GMT -4. The time now is 00:34.

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