我刚刚开始学习c ++,我正在尝试创建一个简单的mysql数据库连接。虽然我有一个Web开发人员背景设置,但由于没有c ++知识和工作方式,所以一切都很难。无论如何,我设法安装了mysql ++,我从一个教程中得到了一段代码:
#include </usr/local/include/mysql++/mysql++.h>
#include </usr/local/include/mysql++/cmdline.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const char* db = "backseat";
const char* server = "localhost";
const char* user = "root";
const char* pass = "root";
mysqlpp::Connection conn(true);
if ( conn.connection(db, server, user, pass) )
{
mysqlpp::Query query = conn.query('SHOW TABLES');
if ( mysqlpp::StoreQueryResult res = query.store() )
{
cout << "We have: " << endl;
for (size_t i=0; i < res.num_rows(); i++)
{
cout << '\t' << res[i][0] << endl;
}
}
else
{
cerr << "Failed to get item list: " << query.error() << endl;
return 1;
}
return 0;
}
else
{
cerr << "DB connection failed: " << conn.error() << endl;
return 1;
}
}
应该可以工作,但g ++给了我以下输出:
syd@whitebook:~/Desktop$ g++ sql.cpp -o sql -DMYSQLPP_MYSQL_HEADERS_BURIED
sql.cpp:19:37: warning: character constant too long for its type [enabled by default]
sql.cpp: In function ‘int main()’:
sql.cpp:17:12: error: ‘class mysqlpp::Connection’ has no member named ‘connection’
sql.cpp:19:50: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
/usr/local/include/mysql++/connection.h:251:8: error: initializing argument 1 of ‘mysqlpp::Query mysqlpp::Connection::query(const char*)’ [-fpermissive]
syd@whitebook:~/Desktop$
我在这里做错了什么?你也会建议使用mysql ++或其他东西吗?
PS:教程 - 从这里获取指南:http://my-problem-solved.blogspot.gr/2010/12/installing-mysql-server-and-mysql-apl.html
答案 0 :(得分:1)
你有一个错字(缺少)
mysqlpp::Connecion conn(true);
应该是
mysqlpp::Connection conn(true);
^
^
更正此错误时可能还有其他错误,但这是导致编译错误的原因。如果您仔细阅读编译错误消息非常,您会发现它实际上回复了输入错误的类名Connecion
。
鉴于您重写了问题,我相信您想要的功能被称为
conn.connect(db, server, user, pass)
当我使用mysql ++时,这当然对我有用。您也可以使用
conn.connected();
查看您的连接是否正常;
另外
mysqlpp::Query query = conn.query("SHOW TABLES");
而不是
mysqlpp::Query query = conn.query('SHOW TABLES');
或者您可以使用<<
运算符
Query query = conn.query();
query << "SHOW TABLES";