Raised This Month: $51 Target: $400
 12% 

Regarding databases.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 06-17-2018 , 16:19   Regarding databases.
Reply With Quote #1

Hello everyone! I've recently started coding and I think I'm doing pretty well understanding sourcepawn in general. However when it comes to databases, it takes a dark turn.

I have no idea how to use them, and create them, getting data and all this yadayada. If someone could send me an example of how to set up a proper database script I would be really thankful.

I'm doing a project on my own, that's why I'm trying to learn sql. Already read the wiki page, didn't find it helpful enough. I'm trying to do a weapons menu, the selected weapon will be inserted into the database and saved. When the player spawns, the selected weapon will be given.

This is an example, I want a database to save all the information, thank you:
PHP Code:
#include <sourcemod>
#include <sdktools>

char g_sWeapon[MAXPLAYERS 1][32];

#define DEAGLE "weapon_deagle"

public void OnPluginStart()
{
    
RegConsoleCmd("sm_weapon"Cmd_Weapon);
}

public 
Action Cmd_Weapon(int iClientint iArgs)
{
    
Menu hMenu = new Menu(Menu_Weapon);
    
hMenu.SetTitle("Weapons");
    
hMenu.AddItem("""Deagle");
    
hMenu.Display(iClientMENU_TIME_FOREVER);
    
    return 
Plugin_Handled;
}

public 
int Menu_Weapon(Menu hMenuMenuAction hActionint iClientint iParam)
{
    switch (
hAction)
    {
        case 
MenuAction_Select:
        {
            switch (
iParam)
            {
                case 
0g_sWeapon[iClient] = DEAGLE;
            }
            
            
GiveClientWeapon(iClient);
        }
        
        case 
MenuAction_End:
            
delete hMenu;
    }
}

void GiveClientWeapon(int iClient)
{
    
int iWeapon GetPlayerWeaponSlot(iClient2);
    
    
RemovePlayerItem(iClientiWeapon);
    
AcceptEntityInput(iWeapon"Kill");
    
    
GivePlayerItem(iClientg_sKnife[iClient]);

Sincerely -

Armin (Mugiwara).

Last edited by mug1wara; 06-17-2018 at 16:24.
mug1wara is offline
DarkDeviL
SourceMod Moderator
Join Date: Apr 2012
Old 06-17-2018 , 18:52   Re: Regarding databases.
Reply With Quote #2

It seems like this thread is more relevant for the Scripting section than it is for Plugin/Gameplay Ideas and Requests, as you are providing code and thereby looking for "scripting help" rather than having a "request" for someone else to do it all for you.

I have therefore moved the thread into Scripting.
__________________
Mostly known as "DarkDeviL".

Dropbox FastDL: Public folder will no longer work after March 15, 2017!
For more info, see the [SRCDS Thread], or the [HLDS Thread].
DarkDeviL is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 06-17-2018 , 18:59   Re: Regarding databases.
Reply With Quote #3

Oh yes, thank you.
mug1wara is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 06-18-2018 , 07:57   Re: Regarding databases.
Reply With Quote #4

First, you need to setup your database in configs/databases.cfg.

Then, you need to connect to that database OnPluginStart() or OnMapStart() (if you wish, but you must delete the handle onmapend)

PHP Code:

Database HandleDatabase 
null;

public 
void OnPluginStart()
{    
    
Database.Connect(Handle_ConnectDatabase"database"); // the name of the database from databases.cfg
}

public 
void Handle_ConnectDatabase(Database db, const char[] errorany data)
{
    if(
db == null)
    {
        
LogError("(Handle_ConnectDatabase) %s"error);
        
// or setfailstate();
    
}
    else
    {
        
HandleDatabase db;
    }

Now, you should have a successful connection. You asked for database creation. You can create it in Handle_ConnectDatabase();

PHP Code:

public void Handle_ConnectDatabase(Database db, const char[] errorany data)
{
    if(
db == null)
    {
        
LogError("(Handle_ConnectDatabase) %s"error);
        
// or setfailstate();
    
}
    else
    {
        
HandleDatabase db;
        
db.Query(Handle_CreateDatabase"CREATE TABLE IF NOT EXISTS name_table (columns ...)");
    }
}

public 
void Handle_CreateDatabase(Database dbDBResultSet rs, const char[] errorany data)
{
    if(
rs == null)
    {
        
LogError("(Handle_CreateDatabase) %s"error);
    }

Now, you can use HandleDatabase.Query (or HandleDatabase.Transaction with HandleDatabase.Execute) everywhere in your plugin.
Ilusion9 is offline
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 18:20.


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