• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

[SOLVED] Mangos C++ sql

Status
Not open for further replies.

Tommy

Founder
Again, moved to the correct section. World of Warcraft Emulation -> Other -> C++ is a release section.

Search for related code in the source. Not at all hard to find.

Depending on which database you're wanting to do this towards (auth, character, world), you'll have to choose between 3 static variables (?):

Code:
LoginDatabase
CharacterDatabase
WorldDatabase

Those will access database related methods and such. Everything you need to know about these methods is in Database.h.

The basics:

Execute:
Execute will run INSERT, DELETE, UPDATE, TRUNCATE, etc. without arguments.

BELOW ARE ONLY EXAMPLES!
Code:
LoginDatabase.Execute("UPDATE account SET username='Test' WHERE Id=1");
LoginDatabase.Execute("DELETE FROM account WHERE Id=1");

PExecute:
PExecute will run the same as Execute but with arguments.

BELOW ARE ONLY EXAMPLES!
Code:
uint32 id = 1;
LoginDatabase.PExecute("UPDATE account SET username='Test' WHERE Id=%u", id);
LoginDatabase.PExecute("DELETE FROM account WHERE Id=%u", id);

Query:
Query will SELECT without arguments. If we specifically select a row, there's no need in doing a while loop. If we select multiple rows, while loop will be needed.

BELOW ARE ONLY EXAMPLES!
Code:
QueryResult* result = LoginDatabase.Query("SELECT username FROM account WHERE Id='1'"); // No need for while loop
if (result) // if result isn't null
{
    Field* field = result->Fetch(); // Access fetched data
    std::string user = field[0].GetCppString(); // element 0 is username (the first result)
    // Now we can use the 'user' variable that contains account 1's username
}

QueryResult* result2 = LoginDatabase.Query("SELECT username FROM account); // Selecting all rows in account
if (result2) // if result isn't null
{
    do
    {
        // Accessing all account rows to get their username
        Field* field = result2->Fetch(); // Access fetched data
        std::string user = field[0].GetCppString(); // element 0 is username (the first result)
        // Now we can use the 'user' variable that contains all accounts 'username'
    } while (result2->NextRow()) // while loop needed. Get the next row until no more rows exist.
}

PQuery:
PQuery is like Query except you can supply arguments. If we specifically select a row, there's no need in doing a while loop. If we select multiple rows, while loop will be needed.

BELOW ARE ONLY EXAMPLES!
Code:
uint32 id = 1;
QueryResult* result = LoginDatabase.PQuery("SELECT username FROM account WHERE Id=%u", id); // No need for while loop
if (result) // if result isn't null
{
    Field* field = result->Fetch(); // Access fetched data
    std::string user = field[0].GetCppString(); // element 0 is username (the first result)
    // Now we can use the 'user' variable that contains account 1's username
}

However, your best bet is to actually search the source for further knowledge on how to do this.
 

Rochet2

Moderator / Eluna Dev
Important addition to above.
You MUST delete QueryResult* yourself or you will have a memory leak!
You can do that by doing delete result;

Ensure you do it only once after everything you plan to do with the result.

For TrinityCore this is not needed, but mangos and cmangos do not use anything to provide RAII for the query object from what I recall.
 
Status
Not open for further replies.
Top