MySQL数据库接口的VC实现与应用(2)_MySQL
摘要:MySQL数据库作为一种网络数据库性能十分出色,但其在应用软件中使用较少。本文将主要探讨MySQL提供的两种数据库接口 ――(ODBC API和C API)在VC中的应用,并且形成一个类用以封装C API数据库接口的功能。
关键词:MySQL;数据库接口;ODBC API;C API
3 利用MySQL自带的C API函数实现数据库功能调用
由于各个数据库之间的差异,它们所提供的数据库功能也就各有不同。这样,通过ODBC API就不可能完全拥有所有的数据库功能,因而影响了程序对数据库的控制功能,也就不能充分发挥数据库的能力。并且这种统一的接口还是以损失效能为前提的,这就使数据库操作时间延长。所以,为了解决以上问题,MySQL的制造商在提供ODBC驱动程序的基础上,还提供了各种编程环境下的API,其中包括C API。这些API函数很显然能尽可能地发挥数据库的能力,并减少数据库操作的延长时间,但却使程序的通用性受到严重影响。
MySQL提供了一套C API函数,它由一组函数以及一组用于函数的数据类型组成,这些函数与MySQL 服务器进行通信并访问数据库,可以直接操控数据库,因而显著地提高了操控效能。
C API数据类型包括:MYSQL(数据库连接句柄)、MYSQL_RES(查询返回结果集)、MYSQL_ROW(行集)、MYSQL_FIELD(字段信息)、MYSQL_FIELD_OFFSET(字段表的偏移量)、my_ulonglong(自定义的无符号整型数)等;C API提供的函数包括:mysql_close()、mysql_connect()、mysql_query()、mysql_store_result()、mysql_init()等,其中mysql_query()最为重要,能完成绝大部分的数据库操控。
下面将具体讨论数据库操作类CDatabase通过C API的实现以及在VC中的应用。
3.1 CDatabase类的实现
CDatabase类封装了MySQL数据库的功能,因此不具备通用性,只能在对MySQL的应用程序中使用。下面将根据C++要求及规范给出CDatabase类的具体结构以及相关简要介绍:
class CDatabase
{
public:
BOOL UnLockTable(); //解锁
BOOL LockTable(char* TableName,char* PRIORITY); //加锁
int Reload(); //重新登陆,非零时返回错误信息
char* GetState(); //服务器状态
char* GetServerInfo(); //服务器信息
int GetProtocolInfo(); //协议信息
char* GetHostInfo(); //主机信息
char * GetClientInfo(); //客户机信息
char* GetFieldName(int FieldNum); //字段名
BOOL IsEnd(); //是否最后
int DropDB(char *db); //删除数据库,非零时返回错误信息
void SeekData(int offset); //查找指定数据
int CreateDB(char *db); //创建数据库,非零时返回错误信息
void FreeRecord(); //释放结果集
unsigned int GetFieldNum(); //得到字段数
BOOL ConnectDB(Database_Param *p); //连接数据库
MYSQL_ROW GetRecord(); //得到结果(一个记录)
my_ulonglong GetRowNum(); //得到记录数
BOOL SelectDB(Data_Param *para); //选择数据库
BOOL UpdateRecord(Data_Param *para); //更新记录
BOOL SelectRecord(Data_Param *para); //选择记录
BOOL InsertRecord(Data_Param *para); //插入记录
BOOL DelRecord(Data_Param *para); //删除记录
BOOL SelectAll(Data_Param *para); //选择所有记录
char * OutErrors(); //输出错误信息
CDatabase(); //初始化数据库
virtual ~CDatabase(); //关闭数据库连接
private:
MYSQL mysql; //数据库连接句柄
MYSQL_RES *query; //结果集
MYSQL_ROW row; //记录集
MYSQL_FIELD *field; //字段信息(结构体)
BOOL FindSave(char *str); //查找并保存结果集
};
通过CDatabase类中定义的这些功能函数,我们可以通过远程或本机完成对MySQL数据库的绝大部分操控,并且由于定义了解锁和加锁功能,使得应用程序能够多线程或多进程地访问数据库,大大提高了效能。以上函数的具体功能都是通过调用C API函数实现的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When we assemble the computer, although the installation process is simple, we often encounter problems in the wiring. Often, users mistakenly plug the power supply line of the CPU radiator into the SYS_FAN. Although the fan can rotate, it may not work when the computer is turned on. There will be an F1 error "CPUFanError", which also causes the CPU cooler to be unable to adjust the speed intelligently. Let's share the common knowledge about the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard. Popular science on the CPU_FAN, SYS_FAN, CHA_FAN, and CPU_OPT interfaces on the computer motherboard 1. CPU_FANCPU_FAN is a dedicated interface for the CPU radiator and works at 12V

As a modern and efficient programming language, Go language has rich programming paradigms and design patterns that can help developers write high-quality, maintainable code. This article will introduce common programming paradigms and design patterns in the Go language and provide specific code examples. 1. Object-oriented programming In the Go language, you can use structures and methods to implement object-oriented programming. By defining a structure and binding methods to the structure, the object-oriented features of data encapsulation and behavior binding can be achieved. packagemaini

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

The reason for the error is in python. The reason why NotImplementedError() is thrown in Tornado may be because an abstract method or interface is not implemented. These methods or interfaces are declared in the parent class but not implemented in the child class. Subclasses need to implement these methods or interfaces to work properly. How to solve this problem is to implement the abstract method or interface declared by the parent class in the child class. If you are using a class to inherit from another class and you see this error, you should implement all the abstract methods declared in the parent class in the child class. If you are using an interface and you see this error, you should implement all methods declared in the interface in the class that implements the interface. If you are not sure which

As a new operating system launched by Huawei, Hongmeng system has caused quite a stir in the industry. As a new attempt by Huawei after the US ban, Hongmeng system has high hopes and expectations. Recently, I was fortunate enough to get a Huawei mobile phone equipped with Hongmeng system. After a period of use and actual testing, I will share some functional testing and usage experience of Hongmeng system. First, let’s take a look at the interface and functions of Hongmeng system. The Hongmeng system adopts Huawei's own design style as a whole, which is simple, clear and smooth in operation. On the desktop, various

Java allows inner classes to be defined within interfaces and abstract classes, providing flexibility for code reuse and modularization. Inner classes in interfaces can implement specific functions, while inner classes in abstract classes can define general functions, and subclasses provide concrete implementations.

Interface Interface defines abstract methods and constants in Java. The methods in the interface are not implemented, but are provided by the class that implements the interface. The interface defines a contract that requires the implementation class to provide specified method implementations. Declare the interface: publicinterfaceExampleInterface{voiddoSomething();intgetSomething();} Abstract class An abstract class is a class that cannot be instantiated. It contains a mixture of abstract and non-abstract methods. Similar to interfaces, abstract methods in abstract classes are implemented by subclasses. However, abstract classes can also contain concrete methods, which provide default implementations. Declare abstract class: publicabstractcl

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.
