C连接MySQL数据库开发之Windows环境搭建及测试_MySQL
<strong>一、开发环境</strong>
Win8.1 64位、VS2013、MySQL5.5.3764位
MySQL安装目录为:C:/Program Files/MySQL/MySQL Server 5.5
<strong>二、配置工程环境</strong>
首先创建一个控制台空项目,打开VS2013,文件--> 新建项目 --> 常规 --> 选择“空项目”
因为我们要使用MySQL数据库的API接口编程,所以需要将工程的附加头文件搜索目录和附件库文件搜索目录,指向MySQL安装目录对应的位置,下面是我机子上mysql库和头文件目录:

将VS2013工程的附加头文件目录和附加库目录指向上面两个目录。
1> 配置头文件目录
打开工程配置属性窗口--> C/C++ --> 常规 --> 附加包含目录,把mysql的include目录添加到附加包含目录中,如下图所示:


2> 配置库文件目录
打开工程配置窗口--> 链接器--> 常归 --> 附加库目录,把mysql的lib目录添加到附加库目录中,如下图所示:
打开工程配置窗口--> 链接器--> 输入-->附中依赖项,打libmysql.lib静态库添加到工程编译依赖项,如下图所示:
将libmysql.dll动态库拷贝到工程的根目录或Debug目录下:

<strong>三、测试开发环境</strong>
#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <mysql.h>void testQuery(MYSQL *mysql); // 测试查询数据void main(){ MYSQL *mysql = NULL; /*初始化MYSQL连接句柄*/ mysql = mysql_init((MYSQL *)0); if (!mysql) { return; } /* 连接数据库,连接成功返回conn,否则返回NULL 参数1:mysql_init初始化数据库返回的MYSQL句柄 参数2:数据库服务器地址 参数3:数据库用户名 参数4:数据库密码 参数5:数据库名称 参数6:数据库端口,为0表示默认3306 参数7:如果unix_socket不是NULL,字符串指定套接字或应该被使用的命名管道。注意host参数决定连接的类型 参数8:通常是0 */ mysql = mysql_real_connect(mysql, "localhost","root", "root", "test", 0, NULL, 0); if (mysql) { printf("connection succellfull the database!/n"); } else { printf("connection error:%d, %s/n",mysql_errno(mysql), mysql_error(mysql)); } // 查询数据 testQuery(mysql); // 关闭连接 mysql_close(mysql); system("pause");}// 测试查询void testQuery(MYSQL *mysql){ MYSQL_ROW row; MYSQL_RES *res = NULL; MYSQL_FIELD *fields = NULL; int i, field_count; char *sql = "select * from t_user"; int flag = mysql_real_query(mysql, sql, (unsigned long)strlen(sql)); if (flag) { printf("Query error:%d, %s/n",mysql_errno(mysql), mysql_error(mysql)); return; } // 将查询结果读到内存当中 res = mysql_store_result(mysql); // 获取结果集中的所有字段 fields = mysql_fetch_fields(res); // 字段数量 field_count = mysql_field_count(mysql); for (i = 0; i <p><strong>mysql测试数据及表结构:</strong></p> <pre class="brush:php;toolbar:false">DROP TABLE IF EXISTS `t_user`;CREATE TABLE `t_user` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(32) DEFAULT NULL,`age` int(11) DEFAULT NULL,`address` varchar(100) DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;LOCK TABLES `t_user` WRITE;INSERT INTO `t_user` VALUES (1,'zhangsan',22,'hunan'),(2,'lisi',30,'beijin');UNLOCK TABLES;
小结配置步聚:
1> 安装mysql
2> 创建VS工程,配置工程头文件(mysql.h所在目录)和库文件(libmysql.lib所在目录)附加目录,指向mysql对应的目录
3> 将libmysql.dll动态库拷贝到工程根目录或Debug目录
4> 编写测试程序,验证C连接Mysql数据库
----------------------------------------------------------------------优雅的分割线------------------------------------------------------------------------------------
常见错误:
1、main.obj : error LNK2019: 无法解析的外部符号 mysql_init。。。。。
原因是没有在工程当中添加libmysql.lib配置,配置库文件目录
2、无法启动此程序,因为计算机中丢失libmyslq.dll。。。。
将libmysql.dll动态库拷贝到工程的根目录或Debug目录下。
3、未引入windows.h头文件,因为在windows连接mysql是通过socket方式与数据库进行通信的
4、main.obj : error LNK2019: 无法解析的外部符号 _mysql_init@4,该符号在函数 _main 中被引用.....
数据库位数与编译位数不一致,导致在链接时mysql的库函数找不到,比如:我的mysql是64位,提供的库当然是64位的,如果你在VS上用32位的平台去编译就会造成链接时出错。
修改编译平台,工程-->属性-->配置管理器-->在解决方案的工程列表中选择对应的项目,并将其修改成32位或64位,如果没有就新建一个。
工程源码下载:Windows平台C连接MySQL数据库
参考文档:
C/C++连接MySql数据库

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Solving the problem of slow Photoshop startup requires a multi-pronged approach, including: upgrading hardware (memory, solid-state drive, CPU); uninstalling outdated or incompatible plug-ins; cleaning up system garbage and excessive background programs regularly; closing irrelevant programs with caution; avoiding opening a large number of files during startup.

Pull vertical guides in Photoshop: Enable ruler view (View > ruler). Hover the mouse over the vertical edge of the ruler, and then the cursor becomes a vertical line with double arrows and hold and drag the mouse to pull out the reference line. Click Delete by dragging the guide, or hovering it into a cross.

Unable to access MySQL from the terminal may be due to: MySQL service not running; connection command error; insufficient permissions; firewall blocks connection; MySQL configuration file error.

The MySQL connection may be due to the following reasons: MySQL service is not started, the firewall intercepts the connection, the port number is incorrect, the user name or password is incorrect, the listening address in my.cnf is improperly configured, etc. The troubleshooting steps include: 1. Check whether the MySQL service is running; 2. Adjust the firewall settings to allow MySQL to listen to port 3306; 3. Confirm that the port number is consistent with the actual port number; 4. Check whether the user name and password are correct; 5. Make sure the bind-address settings in my.cnf are correct.

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

The secrets to mastering Office software include: understanding different versions and platforms, correctly installing and configuring, proficient in using the software interface, in-depth understanding of feature operations, application collaboration and sharing functions, utilizing templates and styles, mastering advanced skills, and solving common problems. In addition, you need to choose a version that suits your needs, make good use of templates and styles, develop backup habits, and learn shortcut keys and advanced techniques to improve efficiency.
