0

VS2010配置和连接MySQL详解

 3 months ago
source link: http://chenqx.github.io/2015/02/06/Config-and-Connect-MySQL-in-Visual-Studio/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

VS2010配置和连接MySQL详解 | ShinChan's Blog

  本文介绍 VS2010(Microsoft Visual Studio 10.0) 下利用 MySQL 官方 connector API 的配置和连接 MySQL。

  根据自己的系统下载相应的版本。解压或安装后文件夹名字太长,这里将“mysql-connector-c++-noinstall-1.0.5-win32”改为“mysql”。

VS2008 以上的版本都适用

  下面是在 VS2010 版本下的配置过程。

  • 1. 菜单 Project ->property ->c/c++>general ->additional include directories 添加下面两项。
    D:\Microsoft Visual Studio 10.0\library\mysql\include\cppconn;
    D:\Microsoft Visual Studio 10.0\library\mysql\include;
  • 2. 菜单 Project ->property ->linker ->general ->additional include directories 添加下面三项。
    D:\MySQL\MySQL Server 5.6\lib;
    D:\MySQL\MySQL Server 5.6\bin;
    D:\Microsoft Visual Studio 10.0\library\mysql\lib;
  • 3. 菜单 Project ->property ->linker ->input ->additional include directories 添加下面三项。
    mysqlcppconn.lib;
    mysqlcppconn-static.lib;
    libmysql.lib;

  • 4. 将mysql\lib下的mysqlcppconn.dll文件和 $MySQL\bin\libmysql.dll 复制到Windows\system32 文件夹底下。
      这样就配置好了。

创建MySQL数据库

  这里简单建立一个连接测试数据库 contest

mysql>create database contest;
mysql>use contest
mysql>create table test(
> id int(4) not null primary key auto_increment,
> name char(32) not null,;

本地连接和操作MySQL

  头文件定义(系统默认生成):

//Head file Definition
#pragma once
class mysqlWriter
public:
mysqlWriter(void);
~mysqlWriter(void);

  函数定义:

// cpp2mysql.cpp : Defines the entry point for the console application.
cpp2mysql.cpp, Created on Feb, 2015
#version: 1.0
#author: By chenqx @http://chenqx.github.com
#include "StdAfx.h"
#include "mysqlWriter.h"
#include <iostream>
#include<string>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <statement.h>
#include <prepared_statement.h>
using namespace sql;
using namespace std;
mysqlWriter::mysqlWriter(void){
mysql::MySQL_Driver *driver;
Connection *con;
Statement *state;
ResultSet *result;
PreparedStatement *prep_stmt;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root","passwd"); //Connect localhost
state = con->createStatement();
//Set the database being used.
con->setSchema("contest");
prep_stmt = con->prepareStatement("INSERT INTO test(name) VALUES (?)");
//Insert values.
prep_stmt->setString(1, "testvalue");
prep_stmt->execute();
//SQL query
result = state->executeQuery("select * from test");
while(result->next()){
int id = result->getInt("id");
string count = result->getString("name");
cout<<id<<":"<<count<<endl;
// Release.
delete prep_stmt;
delete state;
delete con;
mysqlWriter::~mysqlWriter(void){
//pass

远程连接MySQL

  有时候需要操作非本地其它 PC 上的 MySQL 数据库,在连接前需要对访问的计算机进行配置,添加访问权限。这里提供两种方法:

  • 1. 进入MySQL 安装路径的 bin 目录:
cd $\MySQL\bin\>MySQL -h localhost -u root

  这样应该可以进入MySQL服务器,代码如下:

mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
  • 2. 授予访问权限
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION
  • 3. 修改生效
mysql>FLUSH PRIVILEGES
  • 4. 退出重启MySQL服务器
mysql>EXIT

  这样就可以在其它任何的主机上以root身份登录啦!

  • 1. 在控制台执行 mysql -u root -p mysql,系统提示输入数据库root用户的密码,输入完成后即进入mysql控制台,这个命令的第一个mysql是执行命令,第二个mysql是系统数据名称,不一样的。
  • 2. 在mysql控制台执行:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourPassword' WITH GRANT OPTION;
  • 3. 在mysql控制台执行命令中的 ‘root’@’%’ 可以这样理解: root是用户名,%是主机名或IP地址,这里的%代表任意主机或IP地址,你也可替换成任意其它用户名或指定唯一的IP地址;’yourPassword’是给授权用户指定的登录数据库的密码;另外需要说明一点的是我这里的都是授权所有权限,可以指定部分权限,GRANT具体操作详情见:http://dev.mysql.com/doc/refman/5.1/en/grant.html。

  • 4. 不放心的话可以 在mysql 控制台执行下面命令检查一下用户表里的内容。

select host, user from user;

  设置好后通过 TCP 远程计算机的IP 即可。

con = driver->connect("tcp://xxx.xxx.xxx.xxx:3306", "user","passwd"); //Connect remote TCP + IP

  数据库操作如上节所述。

Further reading

扫描二维码分享到微信朋友圈Loading...Please wait
qrcode.php?url=http%3A%2F%2Fchenqx.github.com%2F2015%2F02%2F06%2FConfig-and-Connect-MySQL-in-Visual-Studio%2F

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK