动态 版块 发帖 消息 我的
小绿叶技术博客
小绿叶技术博客
小绿叶技术Lv154   
c++ 连接数据库 绿叶结界开发部分     

cmake 代码:



project(eisc)
#项目名
cmake_minimum_required(VERSION 3.10)
# 编译要求:cmake 要大于最低版本 3.1 
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
# 设置编译器, 如果没有配置环境变量,可以改为gcc具体文件路径

 include_directories(/datadisk/eisc/server/mysql/mariadb1011/include/mysql/server/)
# include_directories(/datadisk/eisc/server/mysql/mariadb1011/include/mysql/)
# 添加 头文件搜索路径 (mysql.h 搜索路径)

#add_executable("set" set.cpp)
add_executable("sql" sql.cpp)
# 生成可执行文件: 将 test.cpp 编译成 test.exe 可执行文件

target_link_libraries(sql /datadisk/eisc/server/mysql/mariadb1011/lib/libmysqlclient.so)

# rm -rf ./build ; cmake ./ -Bbuild -G "Unix Makefiles" ; cmake --build build
# rm -rf ./build ; cmake ./ -Bbuild ; cmake --build build
# 编译到当前目录下的 build 文件夹
# 也可以不用指定 -G




c++ 代码


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include "mysql.h"
using namespace std;

class MysqlDB
{
private:                                             //  private [ˈp raɪ vət] 私有
    MYSQL mysql;
    MYSQL_ROW row;
    MYSQL_RES *result;
    MYSQL_FIELD *field;
public:
    MysqlDB()
    {
        if( mysql_init( &mysql ) == NULL )          // 判断数据库初始化
        {
            cout << "init error, line: " << __LINE__ << endl;
            exit(-1);
        }
    }

    ~MysqlDB()                                      // 波浪号是析构函数,删除创建的对象时执行。 不会返回任何值和带参数。作用:跳出程序释放资源
    {
        mysql_close( &mysql );
    }

    void connect( string host, string user, string passwd,  string database )
    {
        //成功返回MYSQL指向的指针,失败返回NULL
        if( !mysql_real_connect( &mysql, host.c_str(), user.c_str(), passwd.c_str(), database.c_str(), 0, NULL, 0 ) )
        {
            cout << "connect error, line: " << __LINE__ << endl;
            exit(-1);
        }
    }

    void createTB();
    void add();
    void del();
    void update();
    void print();

};


void MysqlDB::createTB()
{
    string id, name, sex, birthday, CTtable;
    do
    {
        cout << "请输入创建表信息:\n";
        cin >> CTtable;
        string sql = "create table "+ CTtable +"(id int, name varchar(10), sex varchar(20), birthday varchar(20));";
        cout << sql << endl;
        mysql_query( &mysql, sql.c_str() );
        cout << "是否继续(y/n): ";
        cin >> id;
    } while( id == "y" );
}

void MysqlDB::add()
{
    string id, name, sex, birthday,CTtable;

    cout << "请输入写入信息:\n";
    cin >> id >> name >> sex >> birthday;
    string sql = "insert into stu(id,name,sex,birthday) values('" + id + "', '" + name + "', '" + sex + "', '" + birthday + "');";
    cout << sql << endl;
    mysql_query( &mysql, sql.c_str() );
}

void MysqlDB::del()
{
    string id;
    do
    {
        cout << "请输入删除信息的ID:\n";
        cin >> id;
        string sql = "delete from stu where id='" + id +"';";
        cout << sql << endl;
        mysql_query( &mysql, sql.c_str() );
        cout << "是否继续(y/n): ";
        cin >> id;
    } while( id == "y" );
}

void MysqlDB::update()
{
    string id, filed,value;
    do
    {
        cout << "请输入修改信息ID,字段,值:\n";
        cin >> id >> filed >> value;
        string sql = "update stu set " + filed +"='" + value + "' where ID='" + id + "';";
        cout << sql << endl;
        mysql_query( &mysql, sql.c_str() );
        cout << "是否继续(y/n): ";
        cin >> id;
    } while( id == "y" );
}

void MysqlDB::print()
{
    string sql = "select * from stu;";
    //成功返回0
    mysql_query( &mysql, sql.c_str() );
    //获取查询查询结果;成功返回result的指针,失败返回NULL
    result = mysql_store_result( &mysql );
    if( !result )
    {
        cout << "result error, line : " << __LINE__ << endl;
        return ;
    }

    int num;
    num = mysql_num_fields( result );  //返回字段个数
    for( int i = 0; i < num; i++ )
    {
        field = mysql_fetch_field_direct( result, i );  //返回字段类型
        cout << field->name << "\t\t";  //输出字段名
    }
    cout << endl;

    while( row = mysql_fetch_row( result ), row  != NULL )
    {
        for( int i = 0; i < num; i++ )
        {
            cout << row[i] << "\t\t";
        }
        cout << endl;
    }
}

int main()
{
    MysqlDB db;                 // 将 MysqlDB 继承给  db
    db.connect( "127.0.0.1", "enchantment", "eisc.cn", "enchantment" );
    db.print();

    db.createTB();
    db.print();

    db.add();
    db.print();

    return 0;
}







 4  已被阅读了863次  楼主 2023-03-01 21:16:11
回复列表

回复: c++ 连接数据库 绿叶结界开发部分

guest
登录之后更精彩~
Powered by HadSky 7.12.10
© 2015-2024 PuYueTian
您的IP:44.221.66.130,2024-06-20 13:52:15,Processed in 0.01921 second(s).
Powered by HadSky
小绿叶技术博客