Poco c ++ framework database application: Database Connection Pool
The database driver in Poco c ++ is concise, clean, neat, and can be connected to the database. It is encapsulated in this way, and it is easy to use. the following describes how to connect to MySQL. i. Requirement Description: establish a connection pool with the MySQL database and obtain a connection in the connection pool to achieve common addition, deletion, modification, and query of the database. II. Objective Description: Write the ANSI-style code and output the high result to the terminal, three debugging conditions for verifying program validity: 1. system: ubuntu 2.qt or other IDE 3. after mysql is installed, the correct access account and password are provided. Use the IDE: Qt Creator project file pocomysql. pro
QT += core networkQT -= guiTARGET = poco_mysqlCONFIG += consoleCONFIG -= app_bundleDEFINES += CHARTDIR_HIDE_OBSOLETE _CRT_SECURE_NO_WARNINGSINCLUDEPATH += /usr/local/include/Poco -I /usr/include/mysqlLIBS += -L/usr/local/lib -lPocoData -lPocoDataMySQL -lPocoDataSQLite -lPocoCrypto -lPocoUtil -lPocoFoundation -L /usr/lib64/mysql#LIBS += -L/usr/local/lib -lPocoData -lPocoDataSQLite -lPocoFoundation -lPocoCrypto -lPocoUtilSOURCES += \
Mysql. cppmain File
# Include "Poco/String. h "# include" Poco/Format. h "# include" Poco/Exception. h "# include" Poco/Data/StatementImpl. h "# include" Poco/Data/MySQL/Connector. h "# include" Poco/Data/MySQL/MySQLException. h "# include" Poco/Data/Session. h "# include" Poco/Data/SessionPool. h "# include" Poco/Data/SessionFactory. h "# include" Poco/Data/LOB. h "# include" Poco/Data/MySQL/MySQLStatementImpl. h "# include" Poco/DateTime. h "# include "Poco/Data/RecordSet. h "# include" Poco/Data/Column. h "# include <iostream> using namespace Poco: Data: Keywords; using namespace Poco: Data; using Poco: Data: Session; using Poco: Data :: mySQL: ConnectionException; using Poco: Data: MySQL: StatementException; using Poco: NotFoundException; using Poco: Data: Statement; using Poco: DateTime; using Poco: Data: RecordSet; // The database access information. std: string _ dbConnString = "ho St = localhost; port = 3306; "" user = root; password = 19810311; "" db = smart; "" compress = true; auto-reconnect = true "; int main (int argc, char ** argv) {MySQL: Connector: registerconne(); // create a connection pool Poco: Data: SessionPool pool with the database (MySQL:: Connector: KEY, _ dbConnString, 10); // obtain a database connection pool Poco: Data: Session ses (pool. get (); // if the session with the database is established successfully, the connection information if (ses. isConnected () std: cout <"*** Connected" <'(' <_ DbConnString <')' <std: endl; // if you have a table named ddjj, delete it first, debug ses <"drop table if exists ddjj", now; // Save the query result to std: vector <std: string> names; ses <"show databases", into (names), now; // output query results. All Database names for (std: vector <std: string> :: const_iterator it = names. begin (); it! = Names. end (); ++ it) {std: cout <* it <std: endl;} // create a table named ddjj, field name: name, sex ses <"create table ddjj (name VARCHAR (20), sex VARCHAR (20);", now; // insert DateTime bd (1980, 4, 1); DateTime ld (1982, 5, 9); ses <"insert into ddjj VALUES ('bart Simpson ',?) ", Use (bd), now; ses <" insert into ddjj VALUES ('lisa Simpson ',?) ", Use (ld), now; // query method, and output the query result std: vector <std: string> names1; ses <"select * from ddjj where name like 'bart Simpson '", into (names1), now; for (std: vector <std: string> :: const_iterator it = names1.begin (); it! = Names1.end (); ++ it) {std: cout <"*** tables:" <* it <std: endl ;} statement select (ses); select <"SELECT * FROM ddjj"; select.exe cute (); // create record set RecordSet rs (select); std: size_t cols = rs. columnCount (); // name of the output column for (std: size_t col = 0; col <cols; ++ col) {std: cout <rs. columnName (col) <std: endl;} // output all query results bool more = rs. moveFirst (); while (more) {for (std: size_t col = 0; col <cols; ++ col) {std: cout <rs [col]. convert <std: string> () <"";} std: cout <std: endl; more = rs. moveNext ();} ses. close (); MySQL: Connector: unregisterconne(); return 0 ;}
IV output result *** Connected to (host = localhost; port = 3306; user = root; password = 19810311; db = smart; compress = true; auto-reconnect = true) information_schemamysqlperformance_schemasmart *** tables: Bart SimpsonnamesexBart Simpson 00:00:00 Lisa Simpson 00:00:00