在ubuntu下安装了mysql,mysql++以后,编写了第一个访问数据库的程序:
#include
#include
#include //for system
#include
using namespace std;
int main()
{
mysqlpp::Connection con(false);
con.set_option(new mysqlpp::SetCharsetNameOption("gbk"));
cout << "please input (user root) password: ";
string pwd;
getline(cin, pwd);
if(!con.connect("d2school", "localhost", "root", pwd.c_str()))
{
cout << "can't connect database, please check the password!" << endl;
return -1;
}
mysqlpp::Query query = con.query("select * from user");
mysqlpp::StoreQueryResult res = query.store();
for(unsigned int i = 0; i < res.num_rows(); ++ i)
{
cout << res[i]["name"] << endl;
}
return 0;
}
结果运行时报错:
运行时的日志信息如下:
-------------- Run: Debug in HelloDatabase_Console (compiler: GNU GCC Compiler)---------------
Checking for existence: /home/yanzhenxi/BaiHuaC++/chap04/HelloDatabase_Console/bin/Debug/HelloDatabase_Console
Set variable: LD_LIBRARY_PATH=.:/usr/lib/x86_64-linux-gnu
:/usr/local/lib:
Executing: gnome-terminal -t HelloDatabase_Console -x /usr/bin/cb_console_runner LD_LIBRARY_PATH=:.:/usr/lib/x86_64-linux-gnu
:/usr/local/lib /home/yanzhenxi/BaiHuaC++/chap04/HelloDatabase_Console/bin/Debug/HelloDatabase_Console (in /home/yanzhenxi/BaiHuaC++/chap04/HelloDatabase_Console/.)
Process terminated with status 0 (0 minute(s), 3 second(s))
我以为,是libmysql++-dev没有安装,所以
sudo apt install libmysql++-dev
结果依然是:
我的codeblocks中Global variable配置如下:
工程HelloDatabase_Console的build options如下:
再经过一顿操作之后,问题依然不能解决:
解决方案:
把下面图片中的$(#mysql.lib)移到$(#mysqlpp.lib)下方,或者删除掉,就可以了
第一种方案:把$(#mysql.lib)移动到下方
这种情况下,运行时信息如下:
-------------- Run: Debug in HelloDatabase_Console (compiler: GNU GCC Compiler)---------------
Checking for existence: /home/yanzhenxi/BaiHuaC++/chap04/HelloDatabase_Console/bin/Debug/HelloDatabase_Console
Set variable: LD_LIBRARY_PATH=.:/usr/local/lib:/usr/lib/x86_64-linux-gnu
:
Executing: gnome-terminal -t HelloDatabase_Console -x /usr/bin/cb_console_runner LD_LIBRARY_PATH=:.:/usr/local/lib:/usr/lib/x86_64-linux-gnu
/home/yanzhenxi/BaiHuaC++/chap04/HelloDatabase_Console/bin/Debug/HelloDatabase_Console (in /home/yanzhenxi/BaiHuaC++/chap04/HelloDatabase_Console/.)
Process terminated with status 0 (0 minute(s), 2 second(s))
运行结果为:
第二种方案:把$(#mysql.lib)删掉
第二种方案,运行时信息如下:
-------------- Run: Debug in HelloDatabase_Console (compiler: GNU GCC Compiler)---------------
Checking for existence: /home/yanzhenxi/BaiHuaC++/chap04/HelloDatabase_Console/bin/Debug/HelloDatabase_Console
Set variable: LD_LIBRARY_PATH=.:/usr/local/lib:
Executing: gnome-terminal -t HelloDatabase_Console -x /usr/bin/cb_console_runner LD_LIBRARY_PATH=:.:/usr/local/lib /home/yanzhenxi/BaiHuaC++/chap04/HelloDatabase_Console/bin/Debug/HelloDatabase_Console (in /home/yanzhenxi/BaiHuaC++/chap04/HelloDatabase_Console/.)
Process terminated with status 0 (0 minute(s), 2 second(s))
运行结果如下:
原因分析:
可以看到,这三种情况下的运行时信息的不同之处:
出错情况:
Set variable: LD_LIBRARY_PATH=.:/usr/lib/x86_64-linux-gnu
:/usr/local/lib:
Executing: gnome-terminal -t HelloDatabase_Console -x /usr/bin/cb_console_runner LD_LIBRARY_PATH=:.:/usr/lib/x86_64-linux-gnu
方案一:
Set variable: LD_LIBRARY_PATH=.:/usr/local/lib:/usr/lib/x86_64-linux-gnu
:
Executing: gnome-terminal -t HelloDatabase_Console -x /usr/bin/cb_console_runner LD_LIBRARY_PATH=:.:/usr/local/lib:/usr/lib/x86_64-linux-gnu
方案二:
Set variable: LD_LIBRARY_PATH=.:/usr/local/lib:
Executing: gnome-terminal -t HelloDatabase_Console -x /usr/bin/cb_console_runner LD_LIBRARY_PATH=:.:/usr/local/lib