Jump to content
TrinityCore
miifi

make cpp int = mysql row count

Recommended Posts

I basically have a script, that let's me learn spells from an NPC, but if I am mounted, it doesn't let me learn spells

________________________________

        if (player->IsMounted())
        {
            player->GetSession()->SendAreaTriggerMessage("|cff87CEFAYou must get down of your mount.|r");
            CloseGossipMenuFor(player);
            return false;
        }

________________________________

I would like to turn the "if mounted" into a "if sql table has 30 or more rows"

I imagine it would be something like this, but I don't know the exact coding for it

________________________________

int a =  SELECT COUNT(*) FROM characters_spells

        if (int a >= 30)
        {
            player->GetSession()->SendAreaTriggerMessage("|cff87CEFAYou must get down of your mount.|r");
            CloseGossipMenuFor(player);
            return false;
        }
 

________________________________


Can anyone help out with the code for it please?

Edited by miifi

Share this post


Link to post
Share on other sites

It would be best to avoid SQL queries if you can.
However as you asked for an example of how to make this query, here is one:

#include "DatabaseEnv.h"

int64 count = CharacterDatabase.PQuery("SELECT COUNT(*) FROM characters_spells WHERE guid = %u", player->GetGUID().GetCounter())->Fetch()[0].GetInt64();
if (count >= 30)
{
}

In this code

- the include allows you to use CharacterDatabase variable, which is a global variable.
- PQuery makes a database query and allows you to use printf like syntax to format variables into the SQL.
- GetCounter returns the part of the guid that is usually used in DB for players and items to identify them.
- Fetch returns the current row from the query result, we call this directly as we know that a result will be returned from this specific SQL.
- [0] accesses the first column in the query result. In this case it is the count.
- GetInt64() takes the column data and handles it as an int64 value. Most math in SQL is done with bigint, so using uint32 or similar may actually not work on some machines. Always use proper type!
- the resulting value is stored into a variable "count" and then compared against 30.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...