vs2010连接mysql数据库(含win32和x64两种平台)
数据库安装: 此处有两种安装方式,第一种使用xxx.msi图形化安装方式,和普通的exe软件安装方式一样,按照默认选项一直下一步就可以。mysql下载地址为http://dev.mysql.com/downloads/windows/installer/ 第二种是在mysql官网下载zip压缩包,解压开了就是一
数据库安装:
此处有两种安装方式,第一种使用xxx.msi图形化安装方式,和普通的exe软件安装方式一样,按照默认选项一直下一步就可以。mysql下载地址为http://dev.mysql.com/downloads/windows/installer/
第二种是在mysql官网下载zip压缩包,解压开了就是一个文件夹。zip包下载地址为:http://dev.mysql.com/downloads/mysql/
根据自己的电脑操作系统位数选择相应的版本,否则到时vs链接过不了。
本文只说明第二种安装方式。
1.将mysql-5.6.14-winx64.zip解压缩到C:\mysql目录下,也可以是其它目录,自己随意。进入到C:\mysql\mysql-5.6.14-winx64目录,看到有很多个.ini文件,这个就是数据库的配置文件,不同类型的数据库对应一个.ini文件,你可以设定端口
字符集等等,修改完了之后将文件命名为my.ini,这样mysql server就能识别了。不过如果你没有特殊需要,这个文件是可以不用动的,删除了也可以,所有的配置项mysql自己都有默认值的。
2.运行栏输入cmd,进入命令界面,cd C:\mysql\mysql-5.6.14-winx64\bin,这里放着mysqld.exe命令
将mysql增加到系统服务中:运行命令 mysqld --install 或者 mysqld --install mysql
3.启动mysql服务端:net start mysql (必须启动着 vs才能连接上来,要是数据库连接失败请查看mysql服务是否启动)
4.使用系统管理员身份运行在命令行运行:mysql -uroot 进入之后就可以执行相关的数据库命令了,若只是以mysql进入,则很多命令执行不了,必须以root用户进入,这里没有密码
5.不想使用数据库了就关掉mysql服务,免得占用内存:net stop mysql
删除mysql服务:mysqld --remove mysql
接下来对如何使用MySql的API连接MySql数据库,开发环境为VS2010.
一、VS2010工程设置工作(win32下)
1.首先,建立一个windows应用程序的工程,将项目-->xx属性(xx为自己取的名字)-->配置属性-->C/C++->预处理器->预处理器定义下的_WINDOWS改为_CONSOLE,默认一般已经这样了
2.链接器->系统->子系统 选择为控制台。默认已经这样的就不用动
由于我们要使用Mysql的API,并且我们机子上肯定安装了Mysql数据库,所以我们要将工程的头文件路径指向Mysql安装目录的同文件mysql.h所在的位置,将连接库路径指向libmysql.lib所在的路径,
在我的机子上,Mysql 的安装路径为:C:\mysql\mysql-5.6.14-winx64\include,C:\mysql\mysql-5.6.14-winx64\lib和下面图片不符,自己找自己的目录
高版本的mysql可能没有opt这个目录层次了,只要找到libmysql.lib这个目录就行
我们需要把VS2008的工程中的头文件路径和连接库路径指向上面的两个地方:
将x项目属性页的C/C++->常规->附加包含目录指向:C:\mysql\mysql-5.6.14-winx64\include
将项目属性页的链接器->常规->附加库目录指向:C:\mysql\mysql-5.6.14-winx64\lib
将链接器->输入->附加依赖项中添加libmysql.lib。
如果不设置链接器->输入->附加依赖项中添加libmysql.lib,那么会出现如下的错误:
1>------ 已启动全部重新生成: 项目: MySql-Connect, 配置: Debug Win32 ------
到此处,win32平台已经配置好,可以打开vs写代码连接数据库了,但是x64平台上链接时总是会有以下错误:这是我遇到的问题
error LNK2019: 无法解析的外部符号 _mysql_real_connect@32,该符号在函数 _main 中被引用
后来一想我装的是win7
64位啊,MySQL也是赤裸裸的64位,我用WIN32 项目搞毛线。于是有一个猜想就是,MySQL 64位的lib也是64位的接口。 于是:项目-->xx属性(xx为自己取的名字)--》配置管理器-->活动解决方案平台 下拉后点击新建,会出现自动填写x64,下面一栏不用动,然后将平台改为x64,既可以完成编译链接。
以下是一个简单的例子源代码:工程类型是最简单的windows控制台程序:
//
data_use.cpp : 定义控制台应用程序的入口点。
|

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



You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

The methods for viewing SQL database errors are: 1. View error messages directly; 2. Use SHOW ERRORS and SHOW WARNINGS commands; 3. Access the error log; 4. Use error codes to find the cause of the error; 5. Check the database connection and query syntax; 6. Use debugging tools.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.
