zoukankan      html  css  js  c++  java
  • linux环境下写C++操作mysql(一)

    /*****************
    connect.cpp
    g++ connect.cpp -o connect -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient
    ****************/
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include"mysql.h"
    
    
    class CMysqlInterface
    {
        public:
            CMysqlInterface();
            ~CMysqlInterface();
            
            void mysqlLibInit();
            void mysqlLibDestroy();
            int Connect();
            int Close();
    
        private:
            MYSQL *m_mysqlPtr;
    };
    
    int main()
    {
        printf("version 1.1
    ");
        int iRet = -1;
        CMysqlInterface MysqlObj;
        
        iRet = MysqlObj.Connect();
        if(0 == iRet)
        {
            printf("mysql_real_connect success
    ");
        }
        else
        {
            printf("mysql_real_connect failed
    ");
        }
        return 0;
    }
    
    CMysqlInterface::CMysqlInterface()
    {
        printf("CMysqlInterface
    ");
        m_mysqlPtr = NULL;
        m_mysqlPtr = mysql_init(NULL);
    }
    
    CMysqlInterface::~CMysqlInterface()
    {
        Close();
    
    }
    
    int CMysqlInterface::Close()
    {
        int iRet = 0;
        
        if(NULL != m_mysqlPtr)
        {
            mysql_close(m_mysqlPtr);
            m_mysqlPtr = NULL;
        }
        return iRet;
    }
    
    void CMysqlInterface::mysqlLibDestroy()
    {
        mysql_library_end();
    }
    
    int CMysqlInterface::Connect()
    {    
        printf("Connect
    ");
        int iRet = -1;
        m_mysqlPtr = mysql_real_connect(m_mysqlPtr,"localhost","root","csql","child",0,NULL,0);
        if(m_mysqlPtr)
        {
            iRet = 0;
        }
        return iRet;
    }

    exbot@ubuntu:~/wangqinghe/MySql/20190621/01$ g++ connect.cpp -o connect -I /usr/include/mysql/ -L /usr/lib/mysql/ -Imysqlclient

    /tmp/cceJyiND.o:在函数‘CMysqlInterface::CMysqlInterface()’中:

    connect.cpp:(.text+0xaf):对‘mysql_init’未定义的引用

    /tmp/cceJyiND.o:在函数‘CMysqlInterface::Close()’中:

    connect.cpp:(.text+0x100):对‘mysql_close’未定义的引用

    /tmp/cceJyiND.o:在函数‘CMysqlInterface::mysqlLibDestroy()’中:

    connect.cpp:(.text+0x121):对‘mysql_server_end’未定义的引用

    /tmp/cceJyiND.o:在函数‘CMysqlInterface::Connect()’中:

    connect.cpp:(.text+0x17c):对‘mysql_real_connect’未定义的引用

    collect2: error: ld returned 1 exit status 

    这个问题是因为连接命令出了错误 后面跟的应该是 -l 而不是-L

    输出结果:

    exbot@ubuntu:~/wangqinghe/MySql/20190621/02$ g++ connect.cpp -o connect -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient

    exbot@ubuntu:~/wangqinghe/MySql/20190621/02$ ./connect

    version 1.1

    CMysqlInterface

    Connect

    mysql_real_connect success

  • 相关阅读:
    Jmeterif controller 使用
    转载App测试工具大全
    Jmeter 官方在线文档&Android SDK 官方下载地址
    APP自动化之uiautomator2 +python3 UI自动化
    uiautomatorviewer不支持安卓 9.0或以上,提示:"error: obtaining UI hierachy"解决方法
    调查显示:软件开发公司出现“人才荒”
    浅谈Lean UX:我们到底该怎么设计?
    谷歌工程师再次公布Windows漏洞 并称微软很难合作
    灵活运用AppFlood:提高APP eCPM的10个技巧
    Spring Framework 4.0M1发布,支持JDK 8、Java EE 7
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11066981.html
Copyright © 2011-2022 走看看