[Question] PExecute and three arguments

Hey,

i have a little problem with my own script. I try to update a row in the database. I use PExecute to send the Update Statement:

Player* target;
uint64 targetGuid;
string targetName;

if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
return false;

QueryResult getData = WorldDatabase.PQuery(“SELECT id FROM testtable WHERE characterguid = %u”,player->GetSession()->GetPlayer()->GetGUID());

uint64 fetchid;
do
{
Field* fields = getData->Fetch();
fetchid = fields[0].GetUInt64();
} while (getData->NextRow());

string UpdateField;
UpdateField = “column1”;
WorldDatabase.PExecute(“UPDATE testtable SET %s = %u WHERE id = %u”, UpdateField.c_str(), targetGuid, fetchid);
handler->PSendSysMessage(“UPDATE testtable SET %s = %u WHERE id = %u”, UpdateField.c_str(), targetGuid, fetchid);

For example:

UpdateField is not static. The string change, but in this example it is “column1”.

targetGuid is the GUID from player target. In this example it has the value 2.

fetchid has the value 1.

So, if the sql statement work, then it is: UPDATE testtable SET column1 = 2 WHERE id = 1

but it does not work. It make this statement: UPDATE testtable SET column1 = 2 WHERE id = 0

So i check the fetchid value and with

handler->PSendSysMessage(“Fetchid: %u”, fetchid);
before the PExecute i get the value 1 and not 0. So what change my fetchid from 1 to 0? Does Pexecute accept only two arguments? If i remove targetGuid and write instead the dynamic value a static value in the statement

WorldDatabase.PExecute(“UPDATE testtable SET %s = 2 WHERE id = %u”, UpdateField.c_str(), fetchid);
handler->PSendSysMessage(“UPDATE testtable SET %s = 2 WHERE id = %u”, UpdateField.c_str(), fetchid);
it works. The chathandler say "UPDATE testtable SET column1 = 2 WHERE id = 1. I dont understand it… Need help for it and sorry for my bad english. Its late and my english skills are not so good /emoticons/default_smile.png

Which core? Compiled 32-bit or 64-bit (not positive that matters, but might be)? You likely need to use %lu for the uint64 values, regardless of the core or compile, but giving more details will be useful if that doesn’t fix you.

^This

I suggest you dont use GUID (which is uint64), but instead use GUIDLow (uint32), which is the guid used in database.

GUID consists of the lowguid, highguid and normally entry (for some objects).

You can make the GUID on core side anyways if you need it with:
uint64 guid = MAKE_NEW_GUID(lowguid, 0, HIGHGUID_PLAYER);

Newest rev. compiled in 32bit. So i should use for my variables uint32 instead uint64?

/edit

I change the PExecute

Player* target;
uint64 targetGuid;
string targetName;

if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
return false;

QueryResult getData = WorldDatabase.PQuery(“SELECT id FROM testtable WHERE characterguid = %u”,player->GetSession()->GetPlayer()->GetGUID());

uint32 fetchid;
do
{
Field* fields = getData->Fetch();
fetchid = fields[0].GetUInt32();
} while (getData->NextRow());

string UpdateField;
UpdateField = “column1”;
WorldDatabase.PExecute(“UPDATE testtable SET %s = %lu WHERE id = %u”, UpdateField.c_str(), targetGuid, fetchid);
handler->PSendSysMessage(“UPDATE testtable SET %s = %lu WHERE id = %u”, UpdateField.c_str(), targetGuid, fetchid);

but same result. Now i fetch my id in a uint32 variable calls fetchid. The fetchid is one line above the PExecute = 1. At the PExecute and the PSendSysMessage the fetchid is 0.

/edit2

For testing i adding this code

QueryResult getPlayerLowGuid = CharacterDatabase.PQuery(“SELECT guid FROM characters WHERE guid = %lu”, targetGuid);

do
{
Field* fields = getPlayerLowGuid->Fetch();
newawesomeGuid = fields[0].GetUInt32();
} while (getPlayerLowGuid->NextRow());

and i change the PExecute to

WorldDatabase.PExecute(“UPDATE testtable SET %s = %u WHERE id = %u”, UpdateFeld.c_str(), newawesomeGuid, fetchid);

This works. The targetGuid must be corrupted or something like that. But how i can convert the targetGuid to a uint32 variable without extra QueryResult?

uint32 newaesomeGuid = target->GetSession()->GetPlayer()->GetGUIDLow();

Dont work. The core crash.

Player* target;
uint64 targetGuid;
std::string targetName;

// Guaranteed to get target guid
// May get target
// Returns false if guid (and thus target) not found
if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
    return false;

// GUID_LOPART gets the GUIDLow from a GUID
uint32 lowGuid = GUID_LOPART(targetGuid);

QueryResult getData = WorldDatabase.PQuery("SELECT id FROM testtable WHERE characterguid = %u", lowGuid);
// Check your query for fails! Prevent crash
if (!getData)
    return false;

uint32 fetchid;
do // No need for a loop, one result expected. Should not be possible to get more results!
{
    Field* fields = getData->Fetch();
    fetchid	= fields[0].GetUInt32();
} while (getData->NextRow());

std::string UpdateField = "column1";
WorldDatabase.PExecute("UPDATE testtable SET %s = %u WHERE id = %u", UpdateField.c_str(), lowGuid, fetchid);
handler->PSendSysMessage("UPDATE testtable SET %s = %u WHERE id = %u", UpdateField.c_str(), lowGuid, fetchid);

You are my hero! It works.