1:需要安装数据库的各种头文件以及动态库如:
MySQL-server-community-5.1.58-1.rhel4.i386.rpm
MySQL-client-community-5.1.58-1.rhel4.i386.rpm
MySQL-devel-community-5.1.58-1.rhel4.i386.rpm
MySQL-shared-compat-5.1.58-1.rhel4.i386.rpm (否则安装mysql++会报错,说链接客户端失败)
2:安装mysql++
./configure -> make -> make install
3:编译测试程序
[zhichen@localhost src]$ vi test.cpp
#include "mysql++.h"
using namespace mysqlpp;
using namespace std;
int main()
{
//构造一个Connection类的对象
Connection con(false);
const char* db = "mysql", *server = "127.0.0.1", *user = "pos", *pass = "pos";
if (con.connect(db, server, user, pass))
{
cout << "success" << endl;
string sql = "select * from users";
Query query = con.query(sql);
if (StoreQueryResult rs = query.store())
{
StoreQueryResult::const_iterator it;
for (it = rs.begin(); it != rs.end(); ++it)
{
Row row = *it;
cout << "/t" << row[0] << "/t" << row[1] << endl;
}
}
else
{
cerr << "Failed to get item list: " << endl;
return -1;
}
}
else
{
cout << "Failed to connect DB" << con.error() << endl;
return -1;
}
return 0;
}
g++ -o test test.cpp -I/usr/include/mysql -I/usr/local/include/mysql++/ -L/usr/local/lib -lmysqlpp -lmysqlclient
4:执行./test
如果报错说
./test: error while loading shared libraries: libmysqlpp.so.3: cannot open shared object file: No such file or directory
man ldconfig
vi /etc/ld.so.conf
把mysq++动态库 /usr/local/lib的路径加进去
5:再次执行测试程序