New C++ MySQL Wrapper
Posted in Code
First dog watch, 1 bell (4:53 pm)

I've been writing a replacement C++ wrapper class using the MySQL API. The previous method worked, but was prone to memory overwrites. The syntax is much better now, too. Just include the two header files and:

boost::shared_ptr<MySQL_Server> server = boost::shared_ptr<MySQL_Server>(new MySQL_Server("localhost", "user", "password", "database"));

boost::shared_ptr<Query> = boost::shared_ptr<Query>(new Query(server));

query->setQuery("UPDATE table SET var=val");

if(query->execute()) {
  std::cout << query->getNumberOfAffectedRows();
} else {
  std::cout << query->getError();
}

SELECT query results can be stepped through with the following:

for(unsigned int i=0; i < query->getNumberOfResultRows(); ++i) {
  std::vector<std::string> row = query->getNextRow();
  ...
}

I think I'm going to like this new class a lot more than the old one!

Leave a Comment »

Leave a Reply