AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   SQL Column Issue. (https://forums.alliedmods.net/showthread.php?t=50098)

mysticssjgoku4 01-19-2007 13:17

SQL Column Issue.
 
I have studied Hawk's little tutorial on the new SQLX module and it's function and seem to be having a little trouble when it comes to retrieving data column by column.


Code:
#include <amxmodx> #include <amxmisc> #include <sqlx> #include <EvoRP> //CVARS #define HUD_1_X "evorp_hud_1_x" #define HUD_1_Y "evorp_hud_1_y" #define HUD_1_RED "evorp_hud_1_red" #define HUD_1_BLU "evorp_hud_1_blue" #define HUD_1_GRE "evorp_hud_1_green" #define HUD_2_X "evorp_hud_2_x" #define HUD_2_Y "evorp_hud_2_y" #define HUD_2_RED "evorp_hud_2_red" #define HUD_2_BLU "evorp_hud_2_blue" #define HUD_2_GRE "evorp_hud_2_green" //SQL Vars new Handle:g_SqlTuple new Handle:Query new Handle:SqlConnection new g_Error[512] new g_Query[32][512] //Necessary Vars new g_SteamIDS[32][32] //Data Vars new g_Bank[32] = 0 new g_Wallet[32] = 0 public plugin_init() {     register_plugin("EvoRP - Hud","1.0","Remo Williams")         //CVARS     register_cvar(HUD_1_X,"-1.9")     register_cvar(HUD_1_Y,"0.0")     register_cvar(HUD_1_RED,"0")     register_cvar(HUD_1_BLU,"190")     register_cvar(HUD_1_GRE,"255")     register_cvar(HUD_2_X,"1.0")     register_cvar(HUD_2_Y,"0.0")     register_cvar(HUD_2_RED,"0")     register_cvar(HUD_2_BLU,"190")     register_cvar(HUD_2_GRE,"255") } public client_authorized(id) {     set_task(5.0,"update_data",id,_,_,"b")     get_user_authid(id,g_SteamIDS[id],31) } public client_disconnect(id) {     remove_task(id)     reset_data(id) } public update_data(id) {       g_Query[id][0] = 0     format(g_Query[id],511,"SELECT * FROM users WHERE SteamID=^"%s^"",g_SteamIDS[id])     new data[10][32]     retrieve_data(g_Query[id],data) } stock retrieve_data(query[]="",output[][32]) {     g_SqlTuple = e_core_get_sql_handle()         server_print(query)         new ErrorCode         SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)     if(g_SqlTuple == Empty_Handle)         set_fail_state(g_Error)         Query = SQL_PrepareQuery(SqlConnection,query)         if(!SQL_Execute(Query))     {         SQL_QueryError(Query,g_Error,511)         set_fail_state(g_Error)     }         new i = 0     while(SQL_MoreResults(Query))     {         while(SQL_NumColumns(Query))         {             SQL_ReadResult(Query,i,output[i],31)             server_print("Found data: %s",output[i])             i++         }         SQL_NextRow(Query)     }     SQL_FreeHandle(Query)     SQL_FreeHandle(SqlConnection) } public reset_data(id) {     g_Bank[id] = 0     g_Wallet[id] = 0     g_SteamIDS[id][0] = 0     g_Query[id][0] = 0 }

Now the thing is, there are only four columns in the user's table.
The server retrieves all of the data properly, but it reads me an error message "[MySQL] Invalid Column: 4" from line 95.
Now the thing is, I thought the while() loop was only supposed to go through what is being retrieved only, and not going past that.
This all is still a bit vague to me and was wondering if anyone would take the time out to assist me here.

Thanks in advance.


Main Plugin:
Code:
#include <amxmodx> #include <amxmisc> #include <sqlx> #include <EvoRP> //CVARS #define SQL_HOST "evorp_host" #define SQL_USER "evorp_user" #define SQL_PASS "evorp_pass" #define SQL_DB "evorp_db" new Handle:g_SqlTuple new Handle:SqlConnection new g_Error[512] public plugin_init() {     register_plugin("EvoRP - Core","1.0","Remo Williams")         register_cvar(SQL_HOST,"localhost",FCVAR_PROTECTED)     register_cvar(SQL_USER,"root",FCVAR_PROTECTED)     register_cvar(SQL_PASS,"",FCVAR_PROTECTED)     register_cvar(SQL_DB,"EvoRP",FCVAR_PROTECTED)         set_task(5.0,"sql_test")         server_cmd("exec addons/amxmodx/configs/EvoRP/EvoRP_Config.cfg") } public plugin_natives() {     register_native("e_core_get_sql_handle","get_sql_handle") } public sql_test() {       new Host[64],User[64],Pass[64],Db[64]     get_cvar_string(SQL_HOST,Host,63)     get_cvar_string(SQL_USER,User,63)     get_cvar_string(SQL_PASS,Pass,63)     get_cvar_string(SQL_DB,Db,63)         g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db)         new ErrorCode     SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)         if(SqlConnection == Empty_Handle)         set_fail_state(g_Error)     server_print("______________________________^n")     server_print("Connection Information:")     server_print(" » Host: %s",Host)     server_print(" » User: %s",User)     server_print(" » Pass: ******")     server_print(" » DB: %s",Db)         if(ErrorCode)     {         server_print("^n{EvoRP} Connection: Error")         server_print("{EvoRP} Connection: %s",g_Error)     }     else server_print("^n{EvoRP} Database: Successful")     server_print("______________________________")     g_Error[0] = 0 }     public get_sql_handle() {     return _:g_SqlTuple } public plugin_end() {     //SQL_FreeHandle(g_SqlTuple) }

teame06 01-19-2007 13:34

Re: SQL Column Issue.
 
What is happening is your while loop for SQL_NumColumns(Query). SQL_NumColumns(Query) only return the numbers of columns from the query. So your while loop will always remain true since SQL_NumColumns(Query) will always return 4 for your 4 columns.

Columns numbers when doing SQL_ReadResult always start from 0. So your incrementing past what is valid.

Code:
    new i = 0     new NumColumns = SQL_NumColumns(Query);     while(SQL_MoreResults(Query))     {         while(NumColumns)         {             SQL_ReadResult(Query,i,output[i],31)             server_print("Found data: %s",output[i])             NumColumns--;             i++         }         SQL_NextRow(Query)     }

mysticssjgoku4 01-19-2007 13:41

Re: SQL Column Issue.
 
Oh wow, I didn't know that, haha. Well now I do, thank you.

Another quick question. The plugins executes the queries every 5 seconds for a while, then seems to return another error: Invalid database handle: 0.
Now could that be because of a memory leak? If so, I don't see why this should be happening. A side note: this was hapenning before I changed the code to fix the column error. Could that have been the culprit?

Thanks again in advance.

teame06 01-19-2007 14:06

Re: SQL Column Issue.
 
Code:
stock retrieve_data(query[]="",output[][32]) {     g_SqlTuple = e_core_get_sql_handle()         server_print(query)         new ErrorCode         SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)     if(g_SqlTuple == Empty_Handle)         set_fail_state(g_Error)

It can be this set of code. If SQL_Connect return a 0 or Empty_Handle Then you will get that error. It because it couldn't possible connect to your database. It will return Empty_Handle and you will get Invalid Database Handle: 0

Edit.

You will get the Invalid Database Handle: 0 on native like SQL_PrepareQuery and SQL_Execute. If the Sqlconnection has an Empty_Handle


All times are GMT -4. The time now is 22:19.

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