zoukankan      html  css  js  c++  java
  • Windows下C++连接MySQL

    步骤:
    1. 安装MySQL数据库
    2. 项目属性页->C/C++->常规->附加包含目录:xxxMySQL Server 5.6include
    3. 项目属性页->链接器->常规->附加库目录:xxxMySQL Server 5.6lib
    4. 项目属性页->链接器->输入->附加依赖项:libmysql.lib 将libmysql.dll拷贝到项目中
    5. 引入头文件:#include<Windows.h> #include <mysql.h>
    测试代码:

    #include<iostream>
    #include<Windows.h>
    #include <mysql.h>
    #include<string>
    
    
    using namespace std;
    
    
    int main()
    {
    
        //必备的一个数据结构
        MYSQL mydata;
    
        //初始化数据库
        if (0 == mysql_library_init(0, NULL, NULL)) {
            cout << "mysql_library_init() succeed" << endl;
        }
        else {
            cout << "mysql_library_init() failed" << endl;
            return -1;
        }
    
        //初始化数据结构
        if (NULL != mysql_init(&mydata)) {
            cout << "mysql_init() succeed" << endl;
        }
        else {
            cout << "mysql_init() failed" << endl;
            return -1;
        }
    
        //在连接数据库之前,设置额外的连接选项
        //可以设置的选项很多,这里设置字符集,否则无法处理中文
        if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) {
            cout << "mysql_options() succeed" << endl;
        }
        else {
            cout << "mysql_options() failed" << endl;
            return -1;
        }
    
        //连接数据库
        if (NULL
            != mysql_real_connect(&mydata, "localhost", "root", "root", "test",
                3306, NULL, 0))
            //这里的地址,用户名,密码,端口可以根据自己本地的情况更改
        {
            cout << "mysql_real_connect() succeed" << endl;
        }
        else {
            cout << "mysql_real_connect() failed" << endl;
            return -1;
        }
    
        //sql字符串
        string sqlstr;
    
        //创建一个表
        sqlstr = "CREATE TABLE IF NOT EXISTS `new_paper` (";
        sqlstr += " `NewID` int(11) NOT NULL AUTO_INCREMENT,";
    
        sqlstr += " `NewCaption` varchar(40) NOT NULL,";
    
        sqlstr += " `NewContent` text,";
    
        sqlstr += " `NewTime` datetime DEFAULT NULL,";
    
        sqlstr += " PRIMARY KEY(`NewID`)";
    
        sqlstr += " ) ENGINE = InnoDB DEFAULT CHARSET = utf8";
    
    
        if (0 == mysql_query(&mydata, sqlstr.c_str())) {
            cout << "mysql_query() create table succeed" << endl;
        }
        else {
            cout << "mysql_query() create table failed" << endl;
            mysql_close(&mydata);
            return -1;
        }
    
    
        //向表中插入数据
        for (int i = 0;i < 100;i++)
        {
            sqlstr =
                "INSERT INTO `test`.`new_paper` (`NewID`, `NewCaption`, `NewContent`, `NewTime`) ";
            sqlstr += "VALUES (default, '组织者', '方提出更换存放骨灰存放', '2015-09-01 14:29:51');";
            if (0 == mysql_query(&mydata, sqlstr.c_str())) {
                cout << "mysql_query() insert data succeed" << endl;
            }
            else {
                cout << "mysql_query() insert data failed" << endl;
                mysql_close(&mydata);
                return -1;
            }
        }
    
        //显示刚才插入的数据
        sqlstr = "SELECT `NewID`,`NewCaption`,`NewContent`,`NewTime` FROM `test`.`new_paper`";
        MYSQL_RES *result = NULL;
        if (0 == mysql_query(&mydata, sqlstr.c_str())) {
            cout << "mysql_query() select data succeed" << endl;
    
            //一次性取得数据集
            result = mysql_store_result(&mydata);
            //取得并打印行数
            int rowcount = mysql_num_rows(result);
            cout << "row count: " << rowcount << endl;
    
            //取得并打印各字段的名称
            unsigned int fieldcount = mysql_num_fields(result);
            MYSQL_FIELD *field = NULL;
            for (unsigned int i = 0; i < fieldcount; i++) {
                field = mysql_fetch_field_direct(result, i);
                cout << field->name << "		";
            }
            cout << endl;
    
            //打印各行
            MYSQL_ROW row = NULL;
            row = mysql_fetch_row(result);
            while (NULL != row) {
                for (int i = 0; i < fieldcount; i++) {
                    cout << row[i] << "		";
                }
                cout << endl;
                row = mysql_fetch_row(result);
            }
    
        }
        else {
            cout << "mysql_query() select data failed" << endl;
            mysql_close(&mydata);
            return -1;
        }
    
    
        //删除刚才建的表
        sqlstr = "DROP TABLE `test`.`new_paper`";
        if (0 == mysql_query(&mydata, sqlstr.c_str())) {
            cout << "mysql_query() drop table succeed" << endl;
        }
        else {
            cout << "mysql_query() drop table failed" << endl;
            mysql_close(&mydata);
            return -1;
        }
        mysql_free_result(result);
        mysql_close(&mydata);
        mysql_server_end();
    
        system("pause");
        return 0;
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    yolo_to_onnx ValueError: need more tan 1 value to unpack
    yolo_to_onnx killed
    C++ 实现二维矩阵的加减乘等运算
    Leetcode 1013. Partition Array Into Three Parts With Equal Sum
    Leetcode 1014. Best Sightseeing Pair
    Leetcode 121. Best Time to Buy and Sell Stock
    Leetcode 219. Contains Duplicate II
    Leetcode 890. Find and Replace Pattern
    Leetcode 965. Univalued Binary Tree
    Leetcode 700. Search in a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/ggzone/p/4786411.html
Copyright © 2011-2022 走看看