Mysql VS2015调用

  • VS2015 调用mysql的demo笔记
  • 整理了下网上的内容,这里记一下
    1.创建任意终端程序;
    2.指定包含头文件路径:
    Project->properties->C/C++->Additional Include Directories->(这里指定mysql各种头文件的目录,我的是mysql文件夹里的include)
    3.指定lib路径:
    Project->properties->Linker->General->Additional Library Directories->(这里是libmysql.lib所在路径,我的是在lib里)
    4.添加lib:
    Project->properties->Linker->Input->Additional Dependencies->增加libmysql.lib
    5.修改配置:
    Build->Configuration Manager->Active solution platform->x64
    (为了避免模块计算机类型x64与目标计算机类型x86冲突

    6.将libmysql.dll放到新建的终端程序源代码下;

以下为测试代码:

// ConsoleApplication1_mysql_01.cpp : Defines the entry point for the console application.
//

#pragma comment(lib,"libmysql.lib")
#pragma comment(lib, "ws2_32.lib")
#include "stdafx.h"
#include 
#include "mysql.h"
#include 
#include 

#include   
#include   

void sqlselect(MYSQL *mysql, char *command)
{

    int flag = mysql_real_query(mysql, command, strlen(command));

    if (flag)
    {
        printf("Select error:%d, %s\n", mysql_errno(mysql), mysql_error(mysql));
        return;
    }

    MYSQL_RES *res = mysql_store_result(mysql); //读取将查询结果   
    MYSQL_FIELD *field = mysql_fetch_fields(res); //获取所有列名
    int field_count = mysql_field_count(mysql); //获取列数

                                                //输出所有列名
    for (int i = 0; i < field_count; i++)
    {
        printf("%s\t", field[i].name);
    }

    printf("\n");

    //遍历输出每一行数据  
    MYSQL_ROW row;
    while (row = mysql_fetch_row(res))
    {
        for (int i = 0; i < field_count; i++)
        {
            printf("%s\t", row[i]);
        }
        printf("\n");
    }
}

int main()
{
    //初始化MySQL连接句柄
    MYSQL *mysql = NULL;
    mysql = mysql_init((MYSQL *)0);

    mysql_real_connect
    (
        mysql,
        "localhost", //数据库地址
        "root", //数据库用户名
        "123456", //数据库密码
        "demo", //数据库名称
        0, //数据库端口,0表示默认端口(即3306)
        NULL, //如果unix_socket不是NULL,字符串指定套接字或应该被使用的命名管道。注意host参数决定连接的类型
        0 //通常是0
    );

    if (!mysql) //连接失败
    {
        printf("Connection error:%d, %s\n", mysql_errno(mysql), mysql_error(mysql));
    }
    else
    {
        printf("hello world\n");
    }

    char *command = "select * from student"; //查询指令

    sqlselect(mysql, command); //查询数据   
    mysql_close(mysql); //关闭连接  

    system("pause");
    return 0;

    return 0;
}

感谢以下作者:
http://blog.csdn.net/daso_csdn/article/details/54646859
https://www.2cto.com/database/201701/587128.html

你可能感兴趣的:(qt学习,mysql,vs2015)