在实际应用中,我们不可能在命令行登录进数据库进行数据的查询、插入等操作,用户一般是使用一个界面良好的应用程序软件来对数据进行管理。为了方便应用程序的开发,MySQL提供了多种
编程语言(C、perl、php等)的编程接口,供开发者使用。 对于C语言来说,MySQL提供了C语言客户机库,它允许从任何C程序的内部访问MySQL
数据库,客户机实现API,API定义了客户机如何建立和执行与服务器的通信。使用C语言开发MySQL项目具有更好的安全性和性能,C语言API是与MySQL一起提供的,包含在mysqlclient库文件中,用于连接和执行数据库查询。 首先看一下MySQL为C语言操作定义的数据类型。 1. MYSQL,MYSQL结构代表一个数据库连接句柄,包含有关服务器的连接状态的信息,几乎所有函数都是用到它。其定 义如下:
typedef struct st_mysql
{
NET net; /* Communication parameters */
unsigned char *connector_fd; /* ConnectorFd for SSL */
char *host,*user,*passwd,*unix_socket,*server_version,*host_info;
char *info, *db;
struct charset_info_st *charset;
MYSQL_FIELD *fields;
MEM_ROOT field_alloc;
my_ulonglong affected_rows;
my_ulonglong insert_id; /* id if insert on table with NEXTNR */
my_ulonglong extra_info; /* Not used */
unsigned long thread_id; /* Id for connection in server */
unsigned long packet_length;
unsigned int port;
unsigned long client_flag,server_capabilities;
unsigned int protocol_version;
unsigned int field_count;
unsigned int server_status;
unsigned int server_language;
unsigned int warning_count;
struct st_mysql_options options;
enum mysql_status status;
my_bool free_me; /* If free in mysql_close */
my_bool reconnect; /* set to 1 if automatic reconnect */
/* session-wide random string */
char scramble[SCRAMBLE_LENGTH+1];
/*
Set if this is the original connection, not a master or a slave we have
added though mysql_rpl_probe() or mysql_set_master()/ mysql_add_slave()
*/
my_bool rpl_pivot;
/*
Pointers to the master, and the next slave connections, points to
itself if lone connection.
*/
struct st_mysql* master, *next_slave;
struct st_mysql* last_used_slave; /* needed for round-robin slave pick */
/* needed for send/read/store/use result to work correctly with replication */
struct st_mysql* last_used_con;
LIST *stmts; /* list of all statements */
const struct st_mysql_methods *methods;
void *thd;
/*
Points to boolean flag in MYSQL_RES or MYSQL_STMT. We set this flag
from mysql_stmt_close if close had to cancel result set of this object.
*/
my_bool *unbuffered_fetch_owner;
/* needed for embedded server - no net buffer to store the 'info' */
char *info_buffer;
void *extension;
} MYSQL; 2. MYSQL_RES MYSQL_RES结构代表返回行的查询结果(SELECT、SHOW、DESCRIBE等),从数据库读取数据,最后就是从MYSQL_RES中读取数据。 MYSQL_RES定义如下:
typedef struct st_mysql_res {
my_ulonglong row_count;
MYSQL_FIELD *fields;
MYSQL_DATA *data;
MYSQL_ROWS *data_cursor;
unsigned long *lengths; /* column lengths of current row */
MYSQL *handle; /* for unbuffered reads */
const struct st_mysql_methods *methods;
MYSQL_ROW row; /* If unbuffered read */
MYSQL_ROW current_row; /* buffer to current row */
MEM_ROOT field_alloc;
unsigned int field_count, current_field;
my_bool eof; /* Used by mysql_fetch_row */
/* mysql_stmt_close() had to cancel this result */
my_bool unbuffered_fetch_cancelled;
void *extension;
} MYSQL_RES; MySQL 有如下C语言API:
| 函数 |
描述 |
| mysql_affected_rows() |
返回上次UPDATE、DELETE或INSERT查询更改/删除/插入的行数。 |
| mysql_autocommit() |
切换 autocommit模式,ON/OFF |
| mysql_change_user() |
更改打开连接上的用户和数据库。 |
| mysql_charset_name() |
返回用于连接的默认字符集的名称。 |
| mysql_close() |
关闭服务器连接。 |
| mysql_commit() |
提交事务。 |
| mysql_connect() |
连接到MySQL服务器。该函数已不再被重视,使用mysql_real_connect()取代。 |