Raised This Month: $32 Target: $400
 8% 

[DBI Module] - Multiple Plugins Using only one MySQL Connection


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
mysticssjgoku4
Veteran Member
Join Date: Jan 2005
Location: Chicago Heights, IL
Old 06-25-2006 , 13:19   [DBI Module] - Multiple Plugins Using only one MySQL Connection
Reply With Quote #1

This tutorial was made solely for personal use. Hopefully this will stop newbies from creating 8 different connections for each "item" plugin they make either for TSRP or even CS.

The files that were used to create this are at the very bottom.
If there are any errors please inform me immediately so I can correct them.
Thank you.

Code:
//>------Step 1 //----------------------------------------------------------------- /* Start off by making yourself a custom include file. */ #if defined _sql_handler_included   #endinput #endif #define _sql_handler_included /* Save this as "sql_handler.inc" inside your compilers include directory.*/ //----------------------------------------------------------------- //>------Step 2 //----------------------------------------------------------------- /* Create your CORE plugin that handles the ACTUAL connecting. */ /*First add your connection variable and make it PUBLIC. */ public Sql:dbc //----------------------------------------------------------------- //>------Step 3 //----------------------------------------------------------------- /* Next add all the included files you'll be needing.*/ /* Notice how we added the include file we just made. */ #include <amxmodx> #include <amxmisc> #include <dbi> #include <sql_handler> //----------------------------------------------------------------- //>------Step 4 //----------------------------------------------------------------- /* Now, we add the initialization of the plugin and create Control Variables(CVARS). */ /* These variables hold the information needed to successfully connect to a database. */ public plugin_init() {     register_plugin("SQLConnecter","1.0","RemoWilliams")     //SQL CVARS     register_cvar("mysql_host","localhost",FCVAR_PROTECTED) // Host Name     register_cvar("mysql_user","root",FCVAR_PROTECTED) // User Name     register_cvar("mysql_pass","",FCVAR_PROTECTED) // Password     register_cvar("mysql_db","mydatabase",FCVAR_PROTECTED) // Database Name } //----------------------------------------------------------------- //>------Step 5 //----------------------------------------------------------------- /* Next, we create the function that makes the connection to the MySQL server */ /* Below is an example of how we would do this properly. */ public sql_connect() {     new host[32], username[32], password[32], dbname[32], error[128]         get_cvar_string("mysql_host",host,32)     get_cvar_string("mysql_user",username,32)     get_cvar_string("mysql_pass",password,32)     get_cvar_string("mysql_db",dbname,32)         dbc = dbi_connect(host,username,password,dbname,error,32)         server_print("^n______________________________________")     server_print("^n[MySQL] SQL Configuration:^n")     server_print(" » Host: %s^n » UserName: %s^n » Password: ** PROTECTED **^n » Database: %s^n",host,username,dbname)         if (dbc == SQL_FAILED)     {         server_print("^n[MySQL] Connection to Database: Failure")         server_print("[MySQL] Connection Error: %s^n",error)     }     else     {         server_print("[MySQL] Connection to Database: Successful!^n")     }     server_print("______________________________________^n") } //----------------------------------------------------------------- //>------Step 6 //----------------------------------------------------------------- /* Since we've created the SQL function, we now need to trigger it somehow */ /* To do this, place this line in the plugin_init() function towards the end */ set_task(5.0,"sql_connect") //----------------------------------------------------------------- //>------Step 7 //----------------------------------------------------------------- /* We have to now create function to return the MySQL handle and status. */ /* We can do this simply */ /* This returns whether or not the connection was successful. */ public bool:sql_status() {     if(dbc <= SQL_FAILED) return false;     if(dbc >= SQL_OK) return true;     return false; } /* This returns the MySQL handle to execute queries */ public sql_handle(id, n) {     return _:dbc }  //----------------------------------------------------------------- //>------Step 8 //----------------------------------------------------------------- /* OOOO Fake Natives */ /* The reason behind these is because we need custom FUNCTION(s) able to be accessed by multiple plugins */ public plugin_natives() {     /* Set the native name first, the function to be targetted once called and it's operation mode */     register_native("mysql_status","sql_status",1)     register_native("mysql_handle", "sql_handle") } //----------------------------------------------------------------- /* Save this file as "sql_connection.sma". */ //>------Step 9 //----------------------------------------------------------------- /* Open back up your custom include file you made earlier named sql_handler.inc */ /* Since we have the plugin made now, we need to add the "Fake Natives" in there to be used by other plugins */ /* In this file we add the custom function name we made when we registered the natives in the sql_connection.sma plugin */ native bool:mysql_status(); native Sql:mysql_handle(); /* Save the Include file */ //----------------------------------------------------------------- /* Congratulations! You've successfully setup a core system that can handle a single SQL connection! */ /* Now you are probably wondering.....how do I use this in another plugin? SIMPLY! */ //>------Step 10 //----------------------------------------------------------------- /* Create one LAST plugin. Name this sql_test.sma */ /* Now whenever you wish to be able to use the SQL connection off of the plugin sql_connection, You need to know something important. YOU NEED TO INCLUDE THE CUSTOM INCLUDE FILE YOU MADE!!! */ /* Now if you don't want to read through the plugin all you need to do is use these two lines To return the DB and result handle. */ new Sql:dbc = mysql_handle() new Result:result = dbi_query(dbc,"%s",query) /* Otherwise, here's the plugin */ /* Heres the plugin */ #include <amxmodx> #include <amxmisc> #include <dbi> #include <sql_handler> public plugin_init() {     register_plugin("SQLTest","1.0","RemoWilliams")     register_concmd("amx_testconnection","sql_test") } public sql_test(id) {        /* Checks to see if connection WAS successful. */     if(mysql_status() == false)     {         client_print(id,print_console,"[MySQL] Connection Failed!")         return PLUGIN_HANDLED     }         /* Returns SQL Handle */     new Sql:dbc = mysql_handle()         /* Uses SQL Handle to create a table named "test" */     new query[256]     format(query,sizeof(query),"CREATE TABLE `test` (`id` int(11) NOT NULL auto_increment,PRIMARY KEY  (`id`));")     dbi_query(dbc,"%s",query)     /*     To execute a query, use this method:     new Result:result = dbi_query(dbc,"%s",query)     if(result >= RESULT_OK)     {         client_print(id,print_chat,"[MYSQL] WORKED W00t!")     }     */     return PLUGIN_HANDLED } /* THATS IT! */ /* Hopefully this will stop people from using 6 different connection for 6 different plugins of the same set */

Files here.
Hope this helped some of you!
Attached Files
File Type: zip sql_handler.zip (353 Bytes, 269 views)
File Type: sma Get Plugin or Get Source (sql_connection.sma - 1256 views - 1.6 KB)
File Type: sma Get Plugin or Get Source (sql_test.sma - 1293 views - 922 Bytes)
__________________


Last edited by mysticssjgoku4; 06-25-2006 at 13:27.
mysticssjgoku4 is offline
Send a message via AIM to mysticssjgoku4 Send a message via MSN to mysticssjgoku4
Shurik3n
Member
Join Date: Feb 2006
Old 07-06-2006 , 22:53   Re: [DBI Module] - Multiple Plugins Using only one MySQL Connection
Reply With Quote #2

http://forums.alliedmods.net/showthread.php?t=24595
Shurik3n is offline
mysticssjgoku4
Veteran Member
Join Date: Jan 2005
Location: Chicago Heights, IL
Old 07-07-2006 , 20:26   Re: [DBI Module] - Multiple Plugins Using only one MySQL Connection
Reply With Quote #3

Ty. Just figured I'd make a tutorial and example for easier understanding.
__________________

mysticssjgoku4 is offline
Send a message via AIM to mysticssjgoku4 Send a message via MSN to mysticssjgoku4
Reply



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 16:22.


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