加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux环境下写C++操作mysql(一)

发布时间:2020-12-13 23:46:45 所属栏目:Linux 来源:网络整理
导读:/* ****************connect.cppg++ 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 : C
/*****************
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.1n");
    int iRet = -1;
    CMysqlInterface MysqlObj;
    
    iRet = MysqlObj.Connect();
    if(0 == iRet)
    {
        printf("mysql_real_connect successn");
    }
    else
    {
        printf("mysql_real_connect failedn");
    }
    return 0;
}

CMysqlInterface::CMysqlInterface()
{
    printf("CMysqlInterfacen");
    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("Connectn");
    int iRet = -1;
    m_mysqlPtr = mysql_real_connect(m_mysqlPtr,"localhost","root","csql","child",0,NULL,0);
    if(m_mysqlPtr)
    {
        iRet = 0;
    }
    return iRet;
}

?

[email?protected]:~/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

?

输出结果:

[email?protected]:~/wangqinghe/MySql/20190621/02$ g++ connect.cpp -o connect -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient

[email?protected]:~/wangqinghe/MySql/20190621/02$ ./connect

version 1.1

CMysqlInterface

Connect

mysql_real_connect success

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读