我刚刚开始学习c ++,目前正在尝试创建简单的mysql db连接。尽管我有网络开发人员的背景知识,但由于没有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

最佳答案

您有错别字(没有)

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";

关于c++ - MySQL++简单脚本编译失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20772996/

10-12 00:45
查看更多