Poco C + + database-driven parts, concise, clean, neat, and database connection, encapsulation into this, or more useful. Here's how to connect to MySQL.
A description of requirements
Establish a connection pool with MySQL database, and get a connection in the connection pool to realize the database common additions and deletions
Ii. Objective Description
Write ANSI-style code and output high results to the terminal, verifying the validity of the program
Three debugging conditions:
1. System: Ubuntu
2.QT or other IDE
3. mysql is installed with the correct access account and password
Four routine description
Using IDE:QT Creator
Project file: Pocomysql.pro
QT + = Core NETWORKQT-= Guitarget = Poco_mysqlconfig + Consoleconfig-= app_bundledefines + = Chartdir_hid E_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.cpp
Main 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::D Ata::keywords;using namespace poco::D ata; Using poco::D Ata::session;using poco::D ata::mysql::connectionexception;using poco::D ata::mysql: : Statementexception;using poco::notfoundexception;using poco::D Ata::statement;using poco::D Atetime;using poco::D ata::recordset;//gives the information to access the database std::string _dbconnstring = "host= localhost;port=3306; " "user=root;password=19810311;" "Db=smart;" "Compress=true;auto-reconnect=true"; Int main (INT&NBSP;ARGC,&NBSP;CHAR**&NBSP;ARGV) { &Nbsp; mysql::connector::registerconnector (); / /Establish a connection pool with the database poco::D ata::sessionpool pool (MySQL::Connector :: key, _dbconnstring,1,32,10); //get a database connection from the data inventory connection pool poco::D ata::session ses (Pool.get ()); //If the session is successfully established with the database, the output connection information if ( Ses.isconnected ()) std::cout << "*** connected to " << "(' << _dbconnstring << ') ' << std::endl; // If there is a table for the name DDJJ, first delete, easy to debug below ses << "Drop table &NBSP;IF&NBSP;EXISTS&NBSP;DDJJ ", now //. Store query results in a container std::vector<std::string> names; ses << "Show databases", Into (names), now; //output query results, All database names are listed here for (std::vector<std::string>::const_ Iterator it = names.begin (); it != names.end (); ++it) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;STD:: cout << *it << std::endl; } // Create a table named DDJJ, field name:name,sex ses << "CREATE&NBSP;TABLE&NBSP;DDJJ (Name varchar),sex VARCHAR ()); ", now; //implementation of data record insertion &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;DATETIME&NBSP;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; //implement the Query method and output the query results 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: " &NBSP;<<&NBSP;*IT&NBSP;<<&NBSP;STD:: endl; } Statement select (SES); select << "select &NBSP;*&NBSP;FROM&NBSP;DDJJ "; select.execute (); //Create record set recordset rs ( Select); std::size_t cols = rs.columncount (); //output Column name for (std::size_t col = 0; col < cols; ++col) { std::cout << rs.columnname (COL) << std::endl; } //output all queries to the 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::unregisterconnector (); return 0;}
Four output results
Connected to (host=localhost;port=3306;user=root;password=19810311;db=smart;compress=true;auto-reconnect=true )
Information_schema
Mysql
Performance_schema
Smart
Tables:bart Simpson
Name
Sex
Bart Simpson 1980-04-01 00:00:00
Lisa Simpson 1982-05-09 00:00:00
This article from "LINUXQT Jinan high-tech Zone" blog, please be sure to keep this source http://qtlinux.blog.51cto.com/3052744/1700516
Poco C + + Framework library application: connection pooling for databases