C/C++ 下mysql应用封装(连接增删改查)

mysql - 初始化

1) mysql_init():初始化数据库

2) mysql_real_connect()(不推荐用Mysql_connect()):连接数据库

详细代码如下:

bool dataBase::initConnection()
{
    try 
    {
        if (mysql_init(&m_sqlCon) == NULL)
        {
            std::cout << "初始化数据库失败" << std::endl;
            return false;
        }
        // localhost:服务器 root为账号密码 test为数据库名 3306为端口    
        if (!mysql_real_connect(&m_sqlCon, host, user, pass,databaseName, port, NULL, 0))
        {
           std::cout << "数据库连接失败" << std::endl;
           return false;
       }
       std::cout << "数据库连接成功" << std::endl;
       return true;
   }
   catch (...)
   {
       std::cout << "出错啦" << std::endl;
       return false;
  }
}

mysql - 查

1)mysql_query():进行查询语句

2)mysql_store_result():获得查询结果

3 ) mysql_fetch_field()以及mysql_fetch_row():获得结果集中的内容

4)mysql_free_result()

代码如下:

void dataBase::user_query(
{
    if (mysql_query(&m_sqlCon, "select * from user"))
    {
        std::cout << "查询失败" << std::endl;
        return;
    }
    std::cout << "查询成功" << std::endl;

    MYSQL_RES *result;
    result = mysql_store_result(&m_sqlCon);
    if (result)
    {
        int row_num, col_num;
        row_num = mysql_num_rows(result);
        col_num = mysql_num_fields(result);
        std::cout << "共有" << row_num << "条数据,以下为其详细内容:" << std::endl;
        MYSQL_FIELD *fd;

        while (fd = mysql_fetch_field(result))
        {
            std::cout << fd->name << "\t";
        }
        std::cout << std::endl;
        MYSQL_ROW sql_row;

        while (sql_row = mysql_fetch_row(result))
        {
            for (int i = 0; i < col_num; i++)
            {
                if (sql_row[i] == NULL) std::cout << "NULL\t";
                else std::cout << sql_row[i] << "\t";
            }
            std::cout << std::endl;
        }
    }

    if (result != NULL)
        mysql_free_result(result);

}

mysql - 增

1)mysql_query()通过select判断是否已经存在(可以省略)

2 ) 通过mysql_query()进行插入

代码如下:

void dataBase::user_insert(UserBody &body)
{
    char select_user[255];
    char query[255];
    MYSQL_RES *result;
    sprintf(select_user, "select * from user where UserName='%s'", body.userName);
    if (mysql_query(&m_sqlCon, select_user) || !(result = mysql_store_result(&m_sqlCon)))
    {
        std::cout << "插入查询失败" << std::endl;
        return;
    }

    if (mysql_num_rows(result))
    {
        std::cout << "用户已存在" << std::endl;
        mysql_free_result(result);
        return;
    }

    mysql_free_result(result);
    sprintf(query, "insert into user(UserName,UserPass,ConnectionNum,UseFor) values('%s','%s',%d,%d)", body.userName, body.userPass, body.connection, body.type);
    if (mysql_query(&m_sqlCon, query))
    {
        std::cout << "插入数据失败" << std::endl;
        return;
    }
    std::cout << "插入数据成功,共插入:" << mysql_affected_rows(&m_sqlCon) << "行" << std::endl;
}

mysql - 改

修改与添加的步骤类似,直接贴出代码就不做赘述了。

代码如下:

void dataBase::user_update(UserBody &body)
{
    char query[255];
    char select_user[255];
    MYSQL_RES *result;
    sprintf(select_user, "select * from user where UserName='%s'", body.userName);
    if (mysql_query(&m_sqlCon, select_user) || !(result = mysql_store_result(&m_sqlCon)))
    {
        std::cout << "修改查询失败" << std::endl;
        return;
    }

    if (mysql_num_rows(result) == 0) {
        std::cout << "要修改的用户不存在" << std::endl;
        mysql_free_result(result);
        return;
    }

    mysql_free_result(result);
    sprintf(query, "update user set UserPass='%s',ConnectionNum=%d,UseFor=%d where UserName='%s'", body.userPass, body.connection, body.type, body.userName);
    if (mysql_query(&m_sqlCon, query))
    {
        std::cout << "修改失败" << std::endl;
        return;
    }
    std::cout << "修改成功,共修改:" << mysql_affected_rows(&m_sqlCon) << "行" << std::endl;
}

mysql - 删

其实删除、修改和插入操作都是类似的,会其中一个其他应该都差不多了,我也就直接贴出代码了。

代码如下:

void dataBase::user_delete(char *userName)
{
    char query[255];
    char select_user[255];
    MYSQL_RES *result;
    sprintf(select_user, "select * from user where UserName='%s'", userName);
    if (mysql_query(&m_sqlCon, select_user) || !(result = mysql_store_result(&m_sqlCon)))
    {
        std::cout << "删除查询失败" << std::endl;
        return;
    }

    if (mysql_num_rows(result) == 0)
    {
        std::cout << "要删除的用户不存在" << std::endl;
        mysql_free_result(result);
        return;
    }

    mysql_free_result(result);
    sprintf(query, "delete from user where UserName='%s'", userName);
    if (mysql_query(&m_sqlCon, query))
    {
        std::cout << "删除失败" << std::endl;
        return;
    }
    std::cout << "删除成功,共删除:" << mysql_affected_rows(&m_sqlCon) << "行" << std::endl;
}

你可能感兴趣的:(Mysql)