开发者社区> 问答> 正文

C++怎么访问远程mysql数据库

已解决

我是一个新手,尝试登录远程mysql时有一些问题:

请问host name应该怎么设置,除了ip地址,可以写http地址吗?
其他的参数应该怎么设置?
另外,我想在C++中使用libmysql++来实现这个功能,请问可以吗?
多谢!

展开
收起
西秦说云 2016-02-01 15:42:30 3890 0
2 条回答
写回答
取消 提交回答
  • 码农|Coder| Pythonista
    采纳回答

    您可以使用http://tangentsoft.net/mysql++/doc/html/userman/
    实例:

    #include "cmdline.h"
    #include "printdata.h"
    
    #include <mysql++.h>
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int
    main(int argc, char *argv[])
    {
        // Get database access parameters from command line
        mysqlpp::examples::CommandLine cmdline(argc, argv);
        if (!cmdline) {
            return 1;
        }
    
        // Connect to the sample database.
        mysqlpp::Connection conn(false);
        if (conn.connect(mysqlpp::examples::db_name, cmdline.server(),
                cmdline.user(), cmdline.pass())) {
            // Retrieve a subset of the sample stock table set up by resetdb
            // and display it.
            mysqlpp::Query query = conn.query("select item from stock");
            if (mysqlpp::StoreQueryResult res = query.store()) {
                cout << "We have:" << endl;
                mysqlpp::StoreQueryResult::const_iterator it;
                for (it = res.begin(); it != res.end(); ++it) {
                    mysqlpp::Row row = *it;
                    cout << '\t' << row[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;
        }
    }
    2019-07-17 18:27:36
    赞同 展开评论 打赏
  • https://github.com/ideal

    也可以使用Mysql的C++ Connector:

    https://dev.mysql.com/doc/connector-cpp/en/

    2019-07-17 18:27:36
    赞同 1 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载
GPON Class C++ SFP OLT Transce 立即下载

相关镜像