Home Database Mysql Tutorial VC++连MySQL小记

VC++连MySQL小记

Jun 07, 2016 pm 04:52 PM

MySQL是开源的,网上很容易就下的到MySQL Dowload.我下的是WINDOWS版的,安装很简单,这里就不多说了,但是别忘了还得下个管理工具.M

传说MySQL性能很好,准备测试一下,如果效果好的话就把数据库换成MySQL.但没想到这一弄就折腾了两个小时.唉!不过还好现在都解决了,恩,现在把前两个小时里学到的东西记在这里,以备不时之需哈

MySQL是开源的,网上很容易就下的到MySQL Dowload.我下的是WINDOWS版的,安装很简单,这里就不多说了,但是别忘了还得下个管理工具.MySQL不像MSSQL那样带有图形界面的管理工具,图形界面程序它只带了重新配置MySQL的MySQLInstanceConfig.exe,剩下的就是一堆命令行程序了.说到重新配置MySQL,我得提醒下,MySQLInstanceConfig.exe如果停在了最后一步Start Service那里报错的话,就从新安装MySQL吧,无论怎么配置都过不了那步.不过具体应该可以手工改好的,不过我没搞懂哈,直接重装就好了.MySQL管理工具有很多,还有PHP版的,我用的是MySQL GUI Tools 5.0,算是官方版的吧,挺好用的

MySQL有提供C的API接口,当然我就直接用的它了,因为传说它比用ADO要快一些.在MySQL的安装目录下你会找到include和lib文件夹(完全安装模式下),里面分别是C接口的头文件和库文件,库文件只用libmysql.lib就好了,头文件的话代码里只需引用mysql.h,当然编译的时候mysql.h还引用了同目录下的其它文件,所以我就把include文件夹里的所有文件都拷贝到VC++的include文件夹里的,只把libmysql.lib拷贝到了VC++的lib文件夹里.

我新建的是个MFC程序,因为MySQL需要网络支持,所以在MFC程序创建向导里要选上"Windows 套接字",否则编译的时候会报SOCKET相关的错误(在这里我折腾了半个小时大概 -.-|||).mysql.h必须在windows.h和sockets头文件之后被引用,MFC里我就把它放到stdafx.h的最后一行了.最后别忘了在连接器参数里加上libmysql.lib的连接,否则会报错:XXX函数未声明.

下面是我用到的MySQL函数:

QUOTE:
MYSQL* mysql_init(NULL) //初始化一个MYSQL对象,后面的操作要用到

MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) //建立连接,db是要访问的数据库,unix_socket和client_flag通常为NULL就可以了.连接成功返回第一个参数,否则失败

int mysql_query(MYSQL *mysql, const char *stmt_str) //执行一个查询,成功时返回0

MYSQL_RES *mysql_store_result(MYSQL *mysql) //获取查询返回的结果集,失败时返回0

unsigned int mysql_num_fields(MYSQL_RES *result) //获取返回结果集的字段数量

MYSQL_ROW mysql_fetch_row(MYSQL_RES *result) //获取一行记录,并将移动到下个记录,返回NULL时则遍历完了所有行或出错.MYSQL_ROW类型是个字符串数组,下标为[0,columnLength-1]

void mysql_free_result(MYSQL_RES *result) //释放结果集

void mysql_close(MYSQL *mysql) //关闭连接


这里是具体代码(写的很粗哈,测试嘛)

QUOTE:
1 MYSQL mysql;
2 mysql_init(&mysql);
3 ASSERT(mysql_real_connect(&mysql, "127.0.0.1", "alacky", "password", "Test", 3306, 0, 0));
4 ASSERT(mysql_query(&mysql, "SELECT * FROM maxTest") == 0);
5 MYSQL_RES *result;
6 result = mysql_store_result(&mysql);
7 MYSQL_ROW row;
8 ULONG colLen = mysql_num_fields(result);
9 CString datas = "";
10 while(row = mysql_fetch_row(result)) // 遍历每行记录
11 {
12 for(ULONG i=0; i 13 {
14 datas += row ? row : "NULL";
15 datas += "\t";
16 }
17 datas += "\n\n";
18 }
19 mysql_free_result(result);
20 mysql_close(&mysql);

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

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.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

See all articles