search
HomeDatabaseMysql TutorialVC++连MySQL小记

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
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
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

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.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft