<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数据库

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

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