This is a read only copy without any forum functionality of the old Modcraft forum.
If there is anything that you would like to have removed, message me on Discord via Kaev#5208.
Big thanks to Alastor for making this copy!

Menu

Author Topic: [C++] Database query  (Read 2277 times)

XxXGenesisXxX

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 204
    • View Profile
[C++] Database query
« on: December 13, 2012, 02:38:27 am »
I'm trying to get this gossip script to get the players characters GUID and a creatures Entry (not GUID) then INSERT them into a table. But I can't seem to have these in the same line:
Code: [Select]
plr->GetGUID(), pCreature->GetEntry()

Which came from this:

Code: [Select]
CharacterDatabase.Execute("INSERT INTO player_shop (ownerguid, vendor) VALUES(%u, %u)", plr->GetGUID(), pCreature->GetEntry());
However all that did is insert the GUID into the ownerguid column, and then set a 0 for the vendor ID. So then I tried this:

Code: [Select]
CharacterDatabase.Execute("INSERT INTO player_shop (ownerguid, vendor) VALUES(%u, %u)", plr->GetGUID(), pCreature->GetGUID());
And it worked as it should. But I don't want the GUID I want the "entry" from the creature table. So then I tried this:

Code: [Select]
CharacterDatabase.Execute("INSERT INTO player_shop (ownerguid, vendor) VALUES(%u, %u)", plr->GetGUID(), pCreature->GetEntry());
CharacterDatabase.Execute("INSERT INTO player_shop (vendor) VALUES(%u)", pCreature->GetEntry());

Which sort of worked, it got both the player character GUID and the creatures entry ID absolutely fine, but as expected it puts it into 2 individual rows.

Also, while the creature_proto and creature_names table use "entry" and I am using "Entry" if I use "entry" in the C++ script it comes says the class hasn't got it as a member. However I fail to see it as important since when on seperate lines it still successfully pulls the "entry" and inserts it into the table.

So can someone please correct this for me and explain why it's screwing up? Is it simply that I can't have the "Get" command twice in one query?
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Steff

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 4551
    • View Profile
Re: [C++] Database query
« Reply #1 on: December 13, 2012, 01:13:13 pm »
Just look if the class pCreature is made form

have a method called GetEntry()
« Last Edit: January 01, 1970, 01:00:00 am by Admin »
Please mark as solved if solved.
Don't ask if you could ask a question... JUST ask the Question.
You can send me also offline messages. I will answer if I get online.
Skype: project.modcraft
Discord: steff#6954

XxXGenesisXxX

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 204
    • View Profile
Re: [C++] Database query
« Reply #2 on: December 13, 2012, 03:42:57 pm »
As I said this works:

Code: [Select]
CharacterDatabase.Execute("INSERT INTO player_shop (vendor) VALUES(%u)", pCreature->GetEntry());
It's only when they are both in the same .Execture they don't seem to.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Steff

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 4551
    • View Profile
Re: [C++] Database query
« Reply #3 on: December 14, 2012, 01:21:00 am »
Perhaps try to use stringstream to build the query and give it to the CharacterDatabase.Execute Method
I personaly only use them. Don´t like the old string stuff;)

Code: [Select]
#include <sstream>
#include <string>

.....

stringstream myQuery;

myQuery << "INSERT INTO player_shop (ownerguid, vendor) VALUES(" << plr->GetGUID() << "," << pCreature->GetGUID() << ")";

CharacterDatabase.Execute( myQuery.toString() );

Perhaps you have to do some

plr->GetGUID().toString() and pCreature->GetGUID().toString() inside the stringstream build if you get type errors there.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »
Please mark as solved if solved.
Don't ask if you could ask a question... JUST ask the Question.
You can send me also offline messages. I will answer if I get online.
Skype: project.modcraft
Discord: steff#6954

XxXGenesisXxX

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 204
    • View Profile
Re: [C++] Database query
« Reply #4 on: December 14, 2012, 06:23:48 am »
Thanks for the reply again Steff. Tried as you said with the stringsstream method, and I do indeed need to add a class for toString to the sstream. I don't suppose you could please elaborate on that, I haven't done much work editing standard library files before.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Steff

  • Administrator
  • Creator of Worlds
  • *****
  • Posts: 4551
    • View Profile
Re: [C++] Database query
« Reply #5 on: December 14, 2012, 07:24:05 am »
I am also no c++ crack. I am fare away from this ;)
Often I jump dump bewtween the code parts and find solutions with luck.

But perhaps I can have a look at your compiler to see what the problem could be. Just chat me on this weekend.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »
Please mark as solved if solved.
Don't ask if you could ask a question... JUST ask the Question.
You can send me also offline messages. I will answer if I get online.
Skype: project.modcraft
Discord: steff#6954

XxXGenesisXxX

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 204
    • View Profile
Re: [C++] Database query
« Reply #6 on: December 14, 2012, 07:47:00 am »
Hehe, that's alright. I'll take annother shot at it tomorrow (been awake for far too long), perhaps I'll figure it out tomorrow. If not I'll come back to this thread.
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

Eatos

  • Registred Member
  • LUA Script Tinker
  • *****
  • Posts: 46
    • View Profile
Re: [C++] Database query
« Reply #7 on: December 14, 2012, 09:19:50 am »
CharacterDatabase.PQuery("INSERT INTO player_shop  VALUES ( '%u', '%u')", plr->GetGUID(), pCreature->GetEntry());

You can also try with PExecute.
CharacterDatabase.PExecute("INSERT INTO player_shop  VALUES ( '%u', '%u')", plr->GetGUID(), pCreature->GetEntry());
« Last Edit: January 01, 1970, 01:00:00 am by Admin »

XxXGenesisXxX

  • Contributors
  • Model Change Addict
  • *****
  • Posts: 204
    • View Profile
Re: [C++] Database query
« Reply #8 on: December 15, 2012, 01:27:24 am »
Quote from: "Eatos"
CharacterDatabase.PQuery("INSERT INTO player_shop  VALUES ( '%u', '%u')", plr->GetGUID(), pCreature->GetEntry());

You can also try with PExecute.
CharacterDatabase.PExecute("INSERT INTO player_shop  VALUES ( '%u', '%u')", plr->GetGUID(), pCreature->GetEntry());

Thanks for the reply Eatos, however i'm using ArcEmu unfortunately. I have no class members for either of those. Besides even on Trinity would that really make a difference? Aren't they just different definitions with basically the same effects?

I will try this though:

CharacterDatabase.Query("INSERT INTO player_shop  VALUES ( '%u', '%u')", plr->GetGUID(), pCreature->GetEntry());
« Last Edit: January 01, 1970, 01:00:00 am by Admin »