zoukankan      html  css  js  c++  java
  • VS2015如何连接mySQL数据库

    mySQL数据库

       
          如题,今天给大家简单演示一下VS2015如何连接mySQL数据库。
          首先呢,大家需要安装vs2015和mySQL这两个软件,我还安装了一个辅助软件SQLyog - 32 bit(我师兄推荐我安装的),还有我的mySQL版本是5.6 。废话少讲,开始了。

    连接mySQL主要考虑两个方面问题,添加mysql.h这个头文件和libmysql.lib这个库文件。
    1.新建win32控制台程序,添加源文件,将测试程序复制进去(我的实在网上随便下载一个测试程序,是原来用来测试VC6.0连接数据库的,不碍事,稍作修改都能用,如果你不想自己去网上找测试程序,那我就把测试程序粘上来吧,如下)
    1. <span style="font-size:12px;">#include <winsock.h>  
    2. #include <iostream>  
    3. #include <string>  
    4. #include <mysql.h>  
    5. using namespace std;  
    6.   
    7. #pragma comment(lib, "ws2_32.lib")  
    8. #pragma comment(lib, "libmysql.lib")  
    9.   
    10. //单步执行,不想单步执行就注释掉  
    11. #define STEPBYSTEP  
    12.   
    13. int main() {  
    14.     cout << "****************************************" << endl;  
    15.   
    16. #ifdef STEPBYSTEP  
    17.     system("pause");  
    18. #endif  
    19.   
    20.     //必备的一个数据结构  
    21.     MYSQL mydata;  
    22.   
    23.     //初始化数据库  
    24.     if (0 == mysql_library_init(0, NULL, NULL)) {  
    25.         cout << "mysql_library_init() succeed" << endl;  
    26.     }  
    27.     else {  
    28.         cout << "mysql_library_init() failed" << endl;  
    29.         return -1;  
    30.     }  
    31.   
    32. #ifdef STEPBYSTEP  
    33.     system("pause");  
    34. #endif  
    35.   
    36.     //初始化数据结构  
    37.     if (NULL != mysql_init(&mydata)) {  
    38.         cout << "mysql_init() succeed" << endl;  
    39.     }  
    40.     else {  
    41.         cout << "mysql_init() failed" << endl;  
    42.         return -1;  
    43.     }  
    44.   
    45. #ifdef STEPBYSTEP  
    46.     system("pause");  
    47. #endif  
    48.   
    49.     //在连接数据库之前,设置额外的连接选项  
    50.     //可以设置的选项很多,这里设置字符集,否则无法处理中文  
    51.     if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) {  
    52.         cout << "mysql_options() succeed" << endl;  
    53.     }  
    54.     else {  
    55.         cout << "mysql_options() failed" << endl;  
    56.         return -1;  
    57.     }  
    58.   
    59. #ifdef STEPBYSTEP  
    60.     system("pause");  
    61. #endif  
    62.   
    63.     //连接数据库  
    64.     if (NULL  
    65.         != mysql_real_connect(&mydata, "localhost", "root", "123\", "test",3306, NULL, 0))  
    66.         //这里的地址,用户名,密码,端口可以根据自己本地的情况更改  
    67.     {  
    68.         cout << "mysql_real_connect() succeed" << endl;  
    69.     }  
    70.     else {  
    71.         cout << "mysql_real_connect() failed" << endl;  
    72.         return -1;  
    73.     }  
    74.   
    75. #ifdef STEPBYSTEP  
    76.     system("pause");  
    77. #endif  
    78.   
    79.     //sql字符串  
    80.     string sqlstr;  
    81.   
    82.     //创建一个表  
    83.     sqlstr = "CREATE TABLE IF NOT EXISTS user_info";  
    84.     sqlstr += "(";  
    85.     sqlstr +=  
    86.         "user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID',";  
    87.     sqlstr +=  
    88.         "user_name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User',";  
    89.     sqlstr +=  
    90.         "user_second_sum INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'";  
    91.     sqlstr += ");";  
    92.     if (0 == mysql_query(&mydata, sqlstr.c_str())) {  
    93.         cout << "mysql_query() create table succeed" << endl;  
    94.     }  
    95.     else {  
    96.         cout << "mysql_query() create table failed" << endl;  
    97.         mysql_close(&mydata);  
    98.         return -1;  
    99.     }  
    100.   
    101. #ifdef STEPBYSTEP  
    102.     system("pause");  
    103. #endif  
    104.   
    105.     //向表中插入数据  
    106.     sqlstr =  
    107.         "INSERT INTO user_info(user_name) VALUES('公司名称'),('一级部门'),('二级部门'),('开发小组'),('姓名');";  
    108.     if (0 == mysql_query(&mydata, sqlstr.c_str())) {  
    109.         cout << "mysql_query() insert data succeed" << endl;  
    110.     }  
    111.     else {  
    112.         cout << "mysql_query() insert data failed" << endl;  
    113.         mysql_close(&mydata);  
    114.         return -1;  
    115.     }  
    116.   
    117. #ifdef STEPBYSTEP  
    118.     system("pause");  
    119. #endif  
    120.   
    121.     //显示刚才插入的数据  
    122.     sqlstr = "SELECT user_id,user_name,user_second_sum FROM user_info";  
    123.     MYSQL_RES *result = NULL;  
    124.     if (0 == mysql_query(&mydata, sqlstr.c_str())) {  
    125.         cout << "mysql_query() select data succeed" << endl;  
    126.   
    127.         //一次性取得数据集  
    128.         result = mysql_store_result(&mydata);  
    129.         //取得并打印行数  
    130.         int rowcount = mysql_num_rows(result);  
    131.         cout << "row count: " << rowcount << endl;  
    132.   
    133.         //取得并打印各字段的名称  
    134.         unsigned int fieldcount = mysql_num_fields(result);  
    135.         MYSQL_FIELD *field = NULL;  
    136.         for (unsigned int i = 0; i < fieldcount; i++) {  
    137.             field = mysql_fetch_field_direct(result, i);  
    138.             cout << field->name << " ";  
    139.         }  
    140.         cout << endl;  
    141.   
    142.         //打印各行  
    143.         MYSQL_ROW row = NULL;  
    144.         row = mysql_fetch_row(result);  
    145.         while (NULL != row) {  
    146.             for (int i = 0; i < fieldcount; i++) {  
    147.                 cout << row[i] << " ";  
    148.             }  
    149.             cout << endl;  
    150.             row = mysql_fetch_row(result);  
    151.         }  
    152.   
    153.     }  
    154.     else {  
    155.         cout << "mysql_query() select data failed" << endl;  
    156.         mysql_close(&mydata);  
    157.         return -1;  
    158.     }  
    159.   
    160. #ifdef STEPBYSTEP  
    161.     system("pause");  
    162. #endif  
    163.   
    164.     //删除刚才建的表  
    165.     sqlstr = "DROP TABLE user_info";  
    166.     if (0 == mysql_query(&mydata, sqlstr.c_str())) {  
    167.         cout << "mysql_query() drop table succeed" << endl;  
    168.     }  
    169.     else {  
    170.         cout << "mysql_query() drop table failed" << endl;  
    171.         mysql_close(&mydata);  
    172.         return -1;  
    173.     }  
    174.     mysql_free_result(result);  
    175.     mysql_close(&mydata);  
    176.     mysql_server_end();  
    177.   
    178.     system("pause");  
    179.     return 0;  
    180. }</span>  
    <span style="font-size:12px;">#include <winsock.h>
    #include <iostream>
    #include <string>
    #include <mysql.h>
    using namespace std;
    
    #pragma comment(lib, "ws2_32.lib")
    #pragma comment(lib, "libmysql.lib")
    
    //单步执行,不想单步执行就注释掉
    #define STEPBYSTEP
    
    int main() {
    	cout << "****************************************" << endl;
    
    #ifdef STEPBYSTEP
    	system("pause");
    #endif
    
    	//必备的一个数据结构
    	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;
    	}
    
    #ifdef STEPBYSTEP
    	system("pause");
    #endif
    
    	//初始化数据结构
    	if (NULL != mysql_init(&mydata)) {
    		cout << "mysql_init() succeed" << endl;
    	}
    	else {
    		cout << "mysql_init() failed" << endl;
    		return -1;
    	}
    
    #ifdef STEPBYSTEP
    	system("pause");
    #endif
    
    	//在连接数据库之前,设置额外的连接选项
    	//可以设置的选项很多,这里设置字符集,否则无法处理中文
    	if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) {
    		cout << "mysql_options() succeed" << endl;
    	}
    	else {
    		cout << "mysql_options() failed" << endl;
    		return -1;
    	}
    
    #ifdef STEPBYSTEP
    	system("pause");
    #endif
    
    	//连接数据库
    	if (NULL
    		!= mysql_real_connect(&mydata, "localhost", "root", "123\", "test",3306, NULL, 0))
    		//这里的地址,用户名,密码,端口可以根据自己本地的情况更改
    	{
    		cout << "mysql_real_connect() succeed" << endl;
    	}
    	else {
    		cout << "mysql_real_connect() failed" << endl;
    		return -1;
    	}
    
    #ifdef STEPBYSTEP
    	system("pause");
    #endif
    
    	//sql字符串
    	string sqlstr;
    
    	//创建一个表
    	sqlstr = "CREATE TABLE IF NOT EXISTS user_info";
    	sqlstr += "(";
    	sqlstr +=
    		"user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID',";
    	sqlstr +=
    		"user_name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User',";
    	sqlstr +=
    		"user_second_sum INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'";
    	sqlstr += ");";
    	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;
    	}
    
    #ifdef STEPBYSTEP
    	system("pause");
    #endif
    
    	//向表中插入数据
    	sqlstr =
    		"INSERT INTO user_info(user_name) VALUES('公司名称'),('一级部门'),('二级部门'),('开发小组'),('姓名');";
    	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;
    	}
    
    #ifdef STEPBYSTEP
    	system("pause");
    #endif
    
    	//显示刚才插入的数据
    	sqlstr = "SELECT user_id,user_name,user_second_sum FROM user_info";
    	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;
    	}
    
    #ifdef STEPBYSTEP
    	system("pause");
    #endif
    
    	//删除刚才建的表
    	sqlstr = "DROP TABLE user_info";
    	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;
    }</span>

    2.添加头文件和库文件
    1' 解决方案资源管理器-->右击工程名-->属性



    2' 配置属性-->C/C++-->常规-->附加包含目录-->左击空白处-->向下箭头-->编辑



    双击空白处-->...-->选择目录-->选择文件夹



    这个目录是拥有mysql.h的目录,根据自己的安装路径不同,位置不同,一般在mysql的安装目录下


    到了这一步,头文件算是添加好了
    下面添加库文件,方法类似
    还是这个界面,C/C++下面一个选项
    链接器-->常规-->附加库目录-->左击空白处-->向下箭头-->编辑-->双击空白处-->...-->选择目录-->选择文件夹-->确定。



    库文件主要是libmysql.lib文件所在文件夹



    添加库文件还需输入
    链接器-->输入-->附加依赖项-->左击右边-->向下箭头-->编辑-->输入libmusql.lib-->确定。


    库文件添加成功!
    简单修改测试程序
    数据库的地址,用户名,密码,端口可以根据自己本地的情况更改

    如果对几个变量意思不理解可以右击函数mysql_real_connect查看定义
    提示:有朋友可能看到我的密码是123\感到是奇怪,看过我以前文章的同学知道我的密码是123,但是大家都知道 加上 不同的字母表示不同的意思,也就是转义字符,C语言中 \ 才表示
    修改完成后编译,成功


    但是有的同学会遇到libmysql.dll文件丢失的问题

    解决方法
    将lib目录下的libmysql.dll拷贝到工程目录下的debug文件下

    到此结束了。
    如有问题,欢迎交流,不正确的地方,欢迎指教
  • 相关阅读:
    HearthBuddy投降插件2019-11-01的使用
    正则表达式在线分析 regex online analyzer
    Tips to write better Conditionals in JavaScript
    The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.
    Cannot capture jmeter traffic in fiddler
    JMETER + POST + anti-forgery token
    input type color
    HearthBuddy修改系统时间
    What are all the possible values for HTTP “Content-Type” header?
    UDK性能优化
  • 原文地址:https://www.cnblogs.com/qixu/p/6140587.html
Copyright © 2011-2022 走看看