Jump to content
  • Advertisement
Sign in to follow this  
rpg gamer 17

wow... everything was so easy but this.

This topic is 3335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Language: C++ Using MSV 2008 Other Details: MySQL Server 5.1 MySQL Connector C++ 1.0.5 I've recently made a post about databases and got the reply MySQL. I downloaded installed read about 20 tutorials and caught on to the whole creating, connecting, and editing a database using C++ and the prompt window. I ran into a problem. I only found one tutorial that showed me how to get data using C++. http://forge.mysql.com/wiki/Connector_C++#Fetching_results When I go to access my table 'WinData' and get the data from column 'Width' I get this error: ---Copied from watch window--- + std::runtime_error {_Str="MySQL_ResultSet::getInt: can't fetch because not on result set" } std::runtime_error ------------------------------ In the MySQL prompt 'DESCRIBE WinData' shows Width to be data type int(11) and that it has a default value of 640. Here is the source code that should show you how I retrieve the data.
//from DataBase.cpp
DataBase::DataBase(void)
{
	DBase = "Islanders"; //Database name
	Table = "WinData"; //Start up table name
	Host = "127.0.0.1"; //local host
	User = "root";
	Password = "*********";
	Message = ""; //Message to send to MySQL
	defaultHost = Host; //defaultvariables
	defaultUser = User;
	defaultPassword = Password;
	defaultDBase = DBase;
	defaultTable = Table;
	defaultMessage = Message;
}
bool DataBase::Initialize(std::string _type)
{
	Message = _type;
	try {
		driver = get_driver_instance(); //get driver
		con = driver->connect(Host, User, Password); //connect to host
		con->setSchema(DBase); //open database
		if(Message != "") //check for start up message
		{
			stmt = con->createStatement();
			pstmt = con->prepareStatement(Message);
			pstmt->executeUpdate();
		}
		return true;
	} catch (sql::SQLException &e) {
		return false;
	}
	return true;
}
int DataBase::GetInt(std::string from, std::string what)
{
	try {
		stmt = con->createStatement();
		res = stmt->executeQuery("select "+what+" from "+from);
		res->next();
		return res->getInt(what);
	} catch (sql::SQLException &e) {
		return false;
	}
}

...
//from manager.cpp
Manager::Manager(void)
{
	Database = new DataBase; //create database class
	CanConnect = Database->Initialize(); //test connection
}
void Manager::Start(void)
{
	if(CanConnect)
	{
		W = Database->GetInt("WinData", "Width");
		H = Database->GetInt("WinData", "Height");
		BPP = Database->GetInt("WinData", "BPP");
		FLAGS = Database->GetUInt("WinData", "Flags");
		FPS = Database->GetInt("WinData", "FrameRate");
		Title = Database->GetString("WinData", "Title");
	}
}




Thanks

Share this post


Link to post
Share on other sites
Advertisement
I don't have a direct answer to your question, but you can check out the MySQL++ library here. It's not a tutorial, but it's a pretty decent and easy to use API for dealing with MySQL. Good luck!

Share this post


Link to post
Share on other sites
I've seen several sites about that but never thought about checking it out. I'll have a look thank you.

[UPDATE]
The table just had default variable set to it. I never inserted any informations into the tables. Basically I was reaching for air. I downloaded MySQL++ and have been expermenting. I edited(remade) my database inorder to cope with the several changes. It is alot easier. Thanks.

[Edited by - rpg gamer 17 on October 23, 2009 8:39:37 PM]

Share this post


Link to post
Share on other sites
EDIT: Ah, so it was an empty table. Carry on.


Original post:
The code looks alright to me, but I only have experience with MySQL libraries for other languages (Java, PHP, et al) to go off. More information would help.

Hmm, do you have the create statements for the database schema? If not, could you get a dump of them with mysqldump? Punch something like this into a command-line interface:

mysqldump -d --user=[your username] --password=[your password] [database name]

Post the schema between source tags in this thread. I s'pose you can further cull any irrelevant tables (hopefully we're only interested in WinData), but don't remove anything that augments that table's behaviour.
We shouldn't need to see the contents of the table, as long as you can make sure there are records in the WinData table and the value you're trying to read in those records isn't currently null, it should suffice.

Also, the interface declarations for those classes you posted would help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!