MySQL Forums
Forum List  »  Connector/C++

fetching string from database
Posted by: Adrian Mar
Date: April 24, 2014 11:17AM

Hello anyone. First, please forgive me my bad language then...please help me resolve my problem. I have little chunk of code:

/* INSERT TUTORIAL CODE HERE! */
sql::Driver *driver = get_driver_instance();
std::auto_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3307", "root", "pass"));
con->setSchema("mybase");
std::auto_ptr<sql::Statement> stmt(con->createStatement());

//example from dev/mysql site

stmt->execute("DROP TABLE IF EXISTS mcountry");
stmt->execute("DROP PROCEDURE IF EXISTS add_ctry");
stmt->execute("DROP PROCEDURE IF EXISTS getdata");

stmt->execute("CREATE TABLE mcountry(country_id INT)");
stmt->execute("CREATE PROCEDURE add_ctry(IN cname INT) BEGIN INSERT INTO mcountry(country_name) VALUES (cname); END;");
stmt->execute("CREATE PROCEDURE getdata() BEGIN SELECT * FROM mcountry; END;");

stmt->execute("CALL add_ctry(112)");
stmt->execute("CALL add_ctry(323)");

stmt->execute("CALL getdata()");

std::auto_ptr<sql::ResultSet> res;

do{
res.reset(stmt->getResultSet());
while(res->next())
{
cout << "ID: " << res->getInt(1);
}
} while(stmt->getMoreResults());

This works fine, I get result as expected but when i change type from INT
to CHAR nothing works. I need fetch string data from my table but instead of
good results I get only strange garbage data. Please help me and thanks for any answers

/* INSERT TUTORIAL CODE HERE! */
sql::Driver *driver = get_driver_instance();
std::auto_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3307", "root", "sqlroot.9194"));
con->setSchema("mybase");
std::auto_ptr<sql::Statement> stmt(con->createStatement());

//example from dev/mysql site

stmt->execute("DROP TABLE IF EXISTS mcountry");
stmt->execute("DROP PROCEDURE IF EXISTS add_ctry");
stmt->execute("DROP PROCEDURE IF EXISTS getdata");

stmt->execute("CREATE TABLE mcountry(country_name CHAR(3))");
stmt->execute("CREATE PROCEDURE add_ctry(IN cname CHAR(3)) BEGIN INSERT INTO mcountry(country_name) VALUES (cname); END;");
stmt->execute("CREATE PROCEDURE getdata() BEGIN SELECT * FROM mcountry; END;");

stmt->execute("CALL add_ctry('abc')");
stmt->execute("CALL add_ctry('def')");

stmt->execute("CALL getdata()");

std::auto_ptr<sql::ResultSet> res;

do{
res.reset(stmt->getResultSet());
while(res->next())
{
cout << "Name: " << res->getString(1);
}
} while(stmt->getMoreResults());

Options: ReplyQuote


Subject
Views
Written By
Posted
fetching string from database
2577
April 24, 2014 11:17AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.