Mysql VS2015调用

3 篇文章 0 订阅
1 篇文章 0 订阅
  • 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 <winsock.h>
#include "mysql.h"
#include <iostream>
#include <stdlib.h>

#include <stdio.h>  
#include <mysql.h>  

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值