I am using a C ++ mysql connector to perform operations in my mysql database.
My C ++ program is a real-time application (rest api) that always runs in the cloud, always waiting for user requests.
When I run my program for the first type, I automatically create a database connection (fields for loading a socket from a configuration file). Example:
conDataBase = new ConDatabase;
if (!conDataBase->Init()) return false;
ConDataBase is a global pointer available for all classes. Function Init():
bool conDatabase::Init()
{
GetParameterStr("DATABASE", "HOST", "", hostname, 255);
db_hostname = hostname;
GetParameterStr("DATABASE", "USER", "", user, 255);
db_user = user;
GetParameterStr("DATABASE", "PASSWORD", "", password, 255);
db_password = password;
GetParameterStr("DATABASE", "SCHEMA", "", schema, 255);
db_schema = schema;
printf("DATABASE: Connecting to %s \n",db_hostname.c_str());
printf("DATABASE: Connecting at %s with user %s \n",db_schema.c_str(), db_user.c_str());
try
{
driver = get_driver_instance();
con = driver->connect(db_hostname.c_str(), db_user.c_str(), db_password.c_str());
con->setSchema(db_schema.c_str());
stmt = con->createStatement();
printf("DATABASE: Connected to database... OK \n");
return true;
}
catch (sql::SQLException &e)
{
std::cout << "# ERR: SQLException in " << __FILE__;
std::cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << std::endl;
std::cout << "# ERR: " << e.what();
std::cout << " (MySQL error code: " << e.getErrorCode();
std::cout << ", SQLState: " << e.getSQLState() << " )" << std::endl;
return false;
}
}
So, when I get a request, for example, to specify userInfo in the userInfo request class, I call the global pointer for the database class as follows:
conDataBase->GetUserInfo(
Inside, GetUserInfo()I create my query as follows:
res = stmt->executeQuery(query);
, : mysqlconnector (res, pstmt, con, etc)?. . , , , . , .. (, mysqlconnector) , , , con, res, etc,, , , , , . , ?