Home > Database > Mysql Tutorial > C API 连接MySql_MySQL

C API 连接MySql_MySQL

WBOY
Release: 2016-06-01 13:49:27
Original
991 people have browsed it

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
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template