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++
2:安裝mysql++
./configure -> make -> make install
3:編譯測試程序
4:執行./test
如果報錯說
5:再次執行測試程序
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;
}
#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:再次執行測試程序