打印本文 打印本文 关闭窗口 关闭窗口
MySQL数据库学习笔记(二)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2753  更新时间:2009/4/22 20:49:06  文章录入:mintao  责任编辑:mintao

1.1.C API

    MySQL随安装包提供用C编程语言编写的客户机库,可以用它编写访问MySQL的客户机程序。这个库定义了应用程序编程接口,包括下面的实用程序:

1.         建立和终止与服务器会话的连接管理例程。

2.         构造查询的例程,将例程发送到服务器,并处理结果。

3.         当其他C API调用失败时,确定错误准确原因的状态和错误报告函数。
实际在使用编程测试中发现,网上下载的4.1.1a-alpha版本动态库中没有进行动态语句处理的函数,下载MySQL5.0的版本用5.0的动态库覆盖4.1的动态库,测试代码运行成功。测试代码见附录一。

1.1.1. 如何执行数据定义语句

可以使用mysql_query或者mysql_real_query函数来实现。

例如:

mysql_query(mysql, "DROP TABLE IF EXISTS test_table")

mysql_query(mysql, "CREATE TABLE test_table(col1 INT,\

                     col2 VARCHAR(40),\

                     col3 SMALLINT,\

                     col4 TIMESTAMP)"

其中mysqlMYSQL类型的指针。范例程序参见附录一。

1.1.2. 如何执行数据操作语句

可以使用mysql_preparemysql_param_count函数来实现。

例如:

char INSERT_SAMPLE []="INSERT INTO test_table(col1,col2,col3) VALUES(?,?,?)"

MYSQL_STMT *stmt= mysql_prepare(mysql, INSERT_SAMPLE, strlen(INSERT_SAMPLE));

/* Get the parameter count from the statement */

       param_count= mysql_param_count(stmt);

范例程序参见附录一。

1.1.3. 如何执行数据库动态操作

可以使用如下函数来实现。

 C API Prepared Statement Function Descriptions.

Function

Description

mysql_prepare()

Prepares an SQL string for execution.

mysql_param_count()

Returns the number of parameters in a prepared SQL statement.

mysql_get_metadata()

Returns prepared statement metadata in the form of a result set.

mysql_bind_param()

Associates application data buffers with the parameter markers in a prepared SQL statement.

mysql_execute()

Executes the prepared statement.

mysql_stmt_affected_rows()

Returns the number of rows changes, deleted, or inserted by the last UPDATE, DELETE, or INSERT query.

mysql_bind_result()

Associates application data buffers with columns in the result set.

mysql_stmt_store_result()

Retrieves the complete result set to the client.

mysql_stmt_data_seek()

Seeks to an arbitrary row number in a statement result set.

mysql_stmt_row_seek()

Seeks to a row offset in a statement result set, using value returned from mysql_stmt_row_tell().

mysql_stmt_row_tell()

Returns the statement row cursor position.

mysql_stmt_num_rows()

Returns total rows from the statement buffered result set.

mysql_fetch()

Fetches the next row of data from the result set and returns data for all bound columns.

mysql_stmt_close()

Frees memory used by prepared statement.

mysql_stmt_errno()

Returns the error number for the last statement execution.

mysql_stmt_error()

Returns the error message for the last statement execution.

mysql_stmt_sqlstate()

Returns the SQLSTATE error code for the last statement execution.

mysql_send_long_data()

Sends long data in chunks to server.

范例程序参见附录一。

1.1.4. 性能测试

测试环境P4 1.8G/512M联想电脑,windowns2000系统,VC6。利用MySQL C API采用预处理mysq_prepare()进行SQL语句操作,模拟网管agt产生moinfo信息文件过程。Moinfo表定义如下:

CREATE TABLE moinfo (

  instanceid int(10) unsigned NOT NULL d

[1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口