|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
环境:VS2013,debug版本,MySQL5.7.17版本
1、安装好MySQL数据库,确保服务器中有MySQL服务,可以在资源管理器中查看
2、打开VS2013,新建一个项目,如果安装的是32位的MySQL,则不需要进行本步,如果安装的是64位的MySQL,则需要在win32控制台上新建一个64位的环境,方法是右击项目,选择属性,右上角有一个配置管理器,点击一下,会有一个活动解决方案平台,新建一个X64即可
3、然后是对MySQL中的include和lib 文件的路径加到VS中
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <WinSock2.h>
- #include "mysql.h"
- #include "winsock.h"
- #include <iomanip>
- using namespace std;
- int res = 0;
- MYSQL_RES *result = NULL;
- MYSQL mysql;
- MYSQL_ROW sql_row;
- MYSQL_FIELD *fd = NULL;
- char column[20][20] = { 0 };
- int selectData(char * SQL, char * Msg);
- int main()
- {
- mysql_init(&mysql);
- if (!mysql_real_connect(&mysql, "localhost", "root", "zqh18362961932", "mysql", 3306, NULL, 0))
- {
- cout << "连接数据库时发生错误!" << endl;
- }
- else
- {
- cout << "连接数据库成功!" << endl;
- /*
- //设置编码格式,否则在cmd下无法显示中文;并且插入的中文也会显示乱码
- mysql_query(&mysql, "SET NAMES GBK");
- char *msg = NULL;
- //查询数据
- char select[] = "select * from user";
- if (selectData(select, msg) != 0)
- {
- cout << msg << endl;
- }
- */
- }
- /*
- if (result != NULL)mysql_free_result(result);//释放结果资源
- mysql_close(&mysql); //释放数据库
- system("pause");
- return(0);
- */
- system("pause");
- //return(0);
- }
- /*
- int selectData(char *SQL, char * Msg)
- {
- res = mysql_query(&mysql, SQL);//查询
- if (!res)
- {
- result = mysql_store_result(&mysql);//保存查询到的数据到result
- if (result)
- {
- int i, j;
- cout << "number of result:" << mysql_num_rows(result)<<endl;
- //获取列名
- for (i = 0; fd = mysql_fetch_field(result); i++)
- {
- strcpy(column[i], fd->name);
- }
- j = mysql_num_fields(result);
- //输出列名
- for (i = 0; i<j; i++)
- {
- cout << setw(10) << setiosflags(iOS::left) << column[i];
- //printf("%s\t", column[i]);
- }
- cout << endl;
- //获取具体的数据
- while (sql_row = mysql_fetch_row(result))
- {
- for (i = 0; i<j; i++)
- {
- cout << setw(10) << setiosflags(ios::left) << sql_row[i];
- //printf("%s\t",sql_row[i]);
- }
- cout << endl;
- }
- }
- return 0;
- }
- else
- {
- Msg = "query sqlfailed!";
- }
- return(1);
- }
- */
复制代码 |
|