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











Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

The open source VNC tool Tigervnc is compatible with a wide range of operating systems, including Windows, Linux, and macOS. This article will introduce in detail the application of Tigervnc on the Debian system. Tigervnc is integrated in the application system of Debian system: In the Debian system, Tigervnc is integrated into the system as a VNC server component. Users can start VNC services through command line tools such as vncserver and customize display settings such as resolution and color depth. Cross-platform connection: Tigervnc client supports Windows, Linux, and macOS, which means users can run this from any

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

In Laravel development, dealing with complex model relationships has always been a challenge, especially when it comes to multi-level BelongsToThrough relationships. Recently, I encountered this problem in a project dealing with a multi-level model relationship, where traditional HasManyThrough relationships fail to meet the needs, resulting in data queries becoming complex and inefficient. After some exploration, I found the library staudenmeir/belongs-to-through, which easily installed and solved my troubles through Composer.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Mac system maintenance includes: disk management (use OmniDiskSweeper to clean disk space, use disk tools to check disk errors) memory management (use Activity Monitor to monitor memory usage, end over-occupying processes) startup item management (use Linc or LaunchControl to manage startup items, disable unnecessary startup items) system cache cleaning (use CleanMyMac X or manually clean system cache) software update (timely update system and applications) regular backup (use Time Machine to backup data regularly) good usage habits (not installing applications excessively, cleaning files regularly, and monitoring system logs)

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)
