Home Database Mysql Tutorial C API 连接MySql_MySQL

C API 连接MySql_MySQL

Jun 01, 2016 pm 01:49 PM
interface computer

bitsCN.com

编译环境:WIN7,VS2010,MYSQL版本5.0
因为VS2010自带的CRecordSet类不能与MYSQL5.0匹配,反正我的电脑上是一创建CRecordSet类VS就崩溃,
所以如果想用ODBC接口的话只能直接调用WINAPI,代码既难看又麻烦
于是根据MYSQL自带手册编写了自己的MYSQL类,调试成功,源代码如下:

pse">mysql.h
/*  <br> * [3/9/2012] <br> * Powered by akaka <br> * <br> * akaka_mysql_h <br> * <br> * C API连接MYSQL数据库 <br> * 一、包含/include 和 /lib <br> * 二、#include<windows.h> (不添加会导致编译时错误) <br> * 三、添加libmysql.lib <br> * 四、设置环境变量:这里直接将/lib 目录下的libmysql.dll复制到c:/windows/system32/ 目录下  <br> * <br> */<br>#pragma once  <br>#include "std_lib_facilities.h"  <br>#include <windows.h>  <br>#include <mysql.h>  <br>  <br>class CMysql{    <br>public:  <br>    CMysql();  <br>    ~CMysql();   <br>    void connect(const char *host, const char *user, const char *passwd,   <br>        const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag);  <br>    void query(const char *query);    <br>    void printinfo ();  <br>    void printrows ();  <br>  <br>private:  <br>    MYSQL* _sql;  <br>    MYSQL_RES* _sqlres;     // result set   <br>private:  <br>    unsigned int _column;   // how many columns in a row in "result set"  <br>};  </mysql.h></windows.h></windows.h>
Copy after login
pse">mysql.cpp
/*  <br> * [3/9/2012] <br> * Powered by akaka <br> * <br> *  <br> * akaka_mysql.cpp <br> */   <br>  <br>#include "akaka_mysql.h"  <br>  <br>//------------------------------------------------------------------------------  <br>  <br>CMysql::CMysql()  <br>{  <br>    if( (_sql = mysql_init(NULL)) == NULL)  <br>    {   throw runtime_error("failed in mysql_init!") ;}  <br>}  <br>  <br>CMysql::~CMysql()  <br>{  <br>    if(_sql)   <br>        mysql_close(_sql);  <br>}  <br>  <br>//------------------------------------------------------------------------------  <br>  <br>void CMysql::connect(const char *host, const char *user, const char *passwd,   <br>    const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)   <br>{  <br>    if (mysql_real_connect(_sql, host, user, passwd, db,  <br>         port, unix_socket, client_flag) == NULL)  <br>         throw runtime_error("failed in mysql_connect!");  <br>  <br>}  <br>  <br>//------------------------------------------------------------------------------   <br>void CMysql::query(const char *query)  <br>{  <br>    // mysql_query Returns:  <br>    // Zero if the query was successful. Non-zero if an error occurred.   <br>    if(mysql_query(_sql, query))  <br>        throw runtime_error("failed in mysql_query!");  <br>  <br>    _sqlres = mysql_store_result(_sql); // 保存结果集  <br>}  <br>  <br>//------------------------------------------------------------------------------    <br>void CMysql::printinfo()  <br>{  <br>    // count how many columns in a row in "result set"  <br>    _column = 0;  <br>       <br>    MYSQL_FIELD* sqlfield;  <br>    while(sqlfield = mysql_fetch_field(_sqlres)) //遍历字段  <br>    {     <br>        cout name         _column++;  <br>    }  <br>    cout }  <br>  <br>//------------------------------------------------------------------------------    <br>void CMysql::printrows()  <br>{  <br>    MYSQL_ROW sqlrow;  <br>    while( sqlrow = mysql_fetch_row(_sqlres)) // 遍历结果集  <br>    {  <br>        for(unsigned int i = 0; i         {  <br>            cout         }  <br>        cout     }     <br>}  
Copy after login
pse">main.cpp
//------------------------------------------------------------------------------  <br>// main  <br>int main()  <br>{  <br>    const char* host = "localhost";  <br>    const char* user = "root";  <br>    const char* passwd = "1121";  <br>    const char* db = "my";  <br>    const unsigned int port = 3306;  <br>    const char* unix_socket = NULL; // If unix_socket is not NULL, the string specifies the socket   <br>                                    // or named pipe that should be used.   <br>                                    // Note that the host parameter determines the type of the connection.   <br>    const unsigned long client_flag = 0;  // The value of client_flag is usually 0, but can be set to a combination of the following flags to enable certain features:   <br>  <br>    try  <br>    {  <br>        CMysql mysql;  <br>        mysql.connect(host,user,passwd,db,port,unix_socket,client_flag);  <br>        mysql.query("SELECT * FROM myclass");  <br>        mysql.printinfo();  <br>        mysql.printrows();  <br>        return 0;  <br>    }catch(runtime_error& e)  <br>    {  <br>        cerr         return 1;  <br>    }  <br>}  
Copy after login

运行结果:

bitsCN.com
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 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)

What should I do if my win11 wallpaper is locked and cannot be changed? What to do if my computer wallpaper is locked and cannot be changed? What should I do if my win11 wallpaper is locked and cannot be changed? What to do if my computer wallpaper is locked and cannot be changed? Jun 09, 2024 pm 08:58 PM

What should I do if my win11 wallpaper is locked and cannot be changed? Users can directly click on this computer, then click on the search box and directly enter %ProgramData%MicrosoftWindows to perform the operation. Now, let this website give users a detailed introduction to the detailed explanation of Win11 wallpaper being locked and unable to be changed. The win11 wallpaper is locked and cannot be changed. Detailed explanation method one: 1. Double-click the mouse to enter [This PC]. 2. After entering, enter a string of codes like [%ProgramData%MicrosoftWindows] in the search box at the top of the page, and then click Enter. 3. Then find [systemdata] in the folder below and click in

How to fine-tune deepseek locally How to fine-tune deepseek locally Feb 19, 2025 pm 05:21 PM

Local fine-tuning of DeepSeek class models faces the challenge of insufficient computing resources and expertise. To address these challenges, the following strategies can be adopted: Model quantization: convert model parameters into low-precision integers, reducing memory footprint. Use smaller models: Select a pretrained model with smaller parameters for easier local fine-tuning. Data selection and preprocessing: Select high-quality data and perform appropriate preprocessing to avoid poor data quality affecting model effectiveness. Batch training: For large data sets, load data in batches for training to avoid memory overflow. Acceleration with GPU: Use independent graphics cards to accelerate the training process and shorten the training time.

deepseek image generation tutorial deepseek image generation tutorial Feb 19, 2025 pm 04:15 PM

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI ​​Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

What should I do if my Win10 password does not meet the password policy requirements? What to do if my computer password does not meet the policy requirements? What should I do if my Win10 password does not meet the password policy requirements? What to do if my computer password does not meet the policy requirements? Jun 25, 2024 pm 04:59 PM

In the Windows 10 system, the password policy is a set of security rules to ensure that the passwords set by users meet certain strength and complexity requirements. If the system prompts that your password does not meet the password policy requirements, it usually means that your password does not meet the requirements set by Microsoft. standards for complexity, length, or character types, so how can this be avoided? Users can directly find the password policy under the local computer policy to perform operations. Let’s take a look below. Solutions that do not comply with password policy specifications: Change the password length: According to the password policy requirements, we can try to increase the length of the password, such as changing the original 6-digit password to 8-digit or longer. Add special characters: Password policies often require special characters such as @, #, $, etc. I

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How to turn off location services in Win10? Tips for turning off the positioning system on your computer How to turn off location services in Win10? Tips for turning off the positioning system on your computer Jun 07, 2024 pm 04:02 PM

When our computer is lost, we can search it through the location service system. Just like the location function of our mobile phones, there are many services that require the location service function to be turned on. However, some friends do not want to turn on the location service function and want to turn it off. But I don’t know how to close it, so let the editor tell you the specific operation method. Interested users can take a look. Solution 1. Click Start in the taskbar below and select "Settings" in the menu options to open. 2. Click the "Privacy" option in the interface that opens. 3. Then click "Privacy" under "App Permissions" on the left. 4.Finally find the right side

How to solve the problem that computer software icons are not displayed on the desktop? In-depth repair guide for software icons not displayed on the computer screen How to solve the problem that computer software icons are not displayed on the desktop? In-depth repair guide for software icons not displayed on the computer screen Jun 10, 2024 pm 05:50 PM

Sometimes when we start the computer, we find that the computer only displays the screen but not the software, icons, taskbar, etc. So how should we solve this problem? Let’s take a look below! Method 1: Check whether the desktop icons are hidden. Sometimes, the icons on the computer desktop are missing, but in fact they are just hidden. You can use the following method to check whether it is hidden. Right-click a blank space on the desktop, select [View], then select [Desktop Icons] from the drop-down menu, making sure that all the required icons are selected. Method 2: Restore desktop icons If the desktop icons are deleted or moved to other locations, you can restore them through the following methods. Right-click an empty space on the desktop, select the [Personalize] option, then select the [Theme] option, and then click [Desktop Icon Settings]. in the pop-up

binance official website URL Binance official website entrance latest genuine entrance binance official website URL Binance official website entrance latest genuine entrance Dec 16, 2024 pm 06:15 PM

This article focuses on the latest genuine entrances to Binance’s official website, including Binance Global’s official website, the US official website and the Academy’s official website. In addition, the article also provides detailed access steps, including using a trusted device, entering the correct URL, double-checking the website interface, verifying the website certificate, contacting customer support, etc., to ensure safe and reliable access to the Binance platform.

See all articles