700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C API 连接MySql【MySQL】

C API 连接MySql【MySQL】

时间:2023-11-06 17:58:35

相关推荐

C API 连接MySql【MySQL】

数据库|mysql教程

,电脑,接口

数据库-mysql教程

自动投注软件源码,vscode怎么进入函数实现,卸载jdk ubuntu,tomcat 创建用户,shp sqlite,网页设计关于我们,dede 数据库 目录,linux 虚拟服务器,jquery html5 插件,前端框架holus,爬虫家里,rename php,seo蒋辉,腾讯springboot文档,dede标签 加入变量,单页网站编辑器,js网页播放声音实现代码兼容各种浏览器,预约刷新模板,网站后台管理系统怎么上传,网上医疗预约挂号页面代码,jsp试题管理系统源码,网络监控程序源代码lzw

编译环境:WIN7,VS,MYSQL版本5.0

因为VS自带的CRecordSet类不能与MYSQL5.0匹配,反正我的电脑上是一创建CRecordSet类VS就崩溃,

所以如果想用ODBC接口的话只能直接调用WINAPI,代码既难看又麻烦

于是根据MYSQL自带手册编写了自己的MYSQL类,调试成功,源代码如下:

易语言创建dx窗口源码,vscode 美化格式,戴尔一体机ubuntu,tomcat系统图解,sqlite 的 api,歌华机房服务器托管,js插件 跑马灯效果,前端vue框架教程,返利 爬虫,php杀码,苏州网络seo公司,h5 css3网站欣赏,网页选项卡切换制作,直播 网页 模板,随机抽奖页面,宿舍管理系统页面,php活动报名管理程序lzw

pse”>mysql.h

棋牌游戏源码破解版,ubuntu如何连wlan,qq爬虫动态头像,php 巴拿马时区,常德seo技术lzw

/*

* [3/9/]

* Powered by akaka

*

* akaka_mysql_h

*

* C API连接MYSQL数据库

* A、包含/include 和 /lib

* B、#include (不添加会导致编译时错误)

* C、添加libmysql.lib

* D、设置环境变量:这里直接将/lib 目录下的libmysql.dll复制到c:/windows/system32/ 目录下

*

*/

#pragma once

#include "std_lib_facilities.h"

#include

#include

class CMysql{

public:

CMysql();

~CMysql();

void connect(const char *host, const char *user, const char *passwd,

const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag);

void query(const char *query);

void printinfo ();

void printrows ();

private:

MYSQL* _sql;

MYSQL_RES* _sqlres;// result set

private:

unsigned int _column; // how many columns in a row in "result set"

};

pse”>mysql.cpp

/*

* [3/9/]

* Powered by akaka

*

*

* akaka_mysql.cpp

*/

#include "akaka_mysql.h"

//------------------------------------------------------------------------------

CMysql::CMysql()

{

if( (_sql = mysql_init(NULL)) == NULL)

{ throw runtime_error("failed in mysql_init!") ;}

}

CMysql::~CMysql()

{

if(_sql)

mysql_close(_sql);

}

//------------------------------------------------------------------------------

void CMysql::connect(const char *host, const char *user, const char *passwd,

const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)

{

if (mysql_real_connect(_sql, host, user, passwd, db,

port, unix_socket, client_flag) == NULL)

throw runtime_error("failed in mysql_connect!");

}

//------------------------------------------------------------------------------

void CMysql::query(const char *query)

{

// mysql_query Returns:

// Zero if the query was successful. Non-zero if an error occurred.

if(mysql_query(_sql, query))

throw runtime_error("failed in mysql_query!");

_sqlres = mysql_store_result(_sql); // 保存结果集

}

//------------------------------------------------------------------------------

void CMysql::printinfo()

{

// count how many columns in a row in "result set"

_column = 0;

MYSQL_FIELD* sqlfield;

while(sqlfield = mysql_fetch_field(_sqlres)) //遍历字段

{

cout << [ <name << ];

_column++;

}

cout << endl;

}

//------------------------------------------------------------------------------

void CMysql::printrows()

{

MYSQL_ROW sqlrow;

while( sqlrow = mysql_fetch_row(_sqlres)) // 遍历结果集

{

for(unsigned int i = 0; i < _column; i++) // 遍历每列

{

cout << [ << sqlrow[i] << ];

}

cout << endl;

}

}

pse”>main.cpp

//------------------------------------------------------------------------------

// main

int main()

{

const char* host = "localhost";

const char* user = "root";

const char* passwd = "1121";

const char* db = "my";

const unsigned int port = 3306;

const char* unix_socket = NULL; // If unix_socket is not NULL, the string specifies the socket

// or named pipe that should be used.

// Note that the host parameter determines the type of the connection.

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:

try

{

CMysql mysql;

mysql.connect(host,user,passwd,db,port,unix_socket,client_flag);

mysql.query("SELECT * FROM myclass");

mysql.printinfo();

mysql.printrows();

return 0;

}catch(runtime_error& e)

{

cerr << e.what() << endl;

return 1;

}

}

运行结果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。