1.1.C API
MySQL随安装包提供用C编程语言编写的客户机库,可以用它编写访问MySQL的客户机程序。这个库定义了应用程序编程接口,包括下面的实用程序:
1. 建立和终止与服务器会话的连接管理例程。
2. 构造查询的例程,将例程发送到服务器,并处理结果。
3. 当其他C API调用失败时,确定错误准确原因的状态和错误报告函数。
实际在使用编程测试中发现,网上下载的4.1.1a-alpha版本动态库中没有进行动态语句处理的函数,下载MySQL5.0的版本用5.0的动态库覆盖4.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)";
其中mysql为MYSQL类型的指针。范例程序参见附录一。
1.1.2. 如何执行数据操作语句可以使用mysql_prepare,mysql_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 default '0',
parentInstanceid int(10) unsigned NOT NULL default '0',
dn tinyblob NOT NULL,
mocid int(10) unsigned NOT NULL default '0',
flag tinyblob NOT NULL,
fid tinyblob NOT NULL,
adminstate tinyint(3) unsigned default '1',
opstate tinyint(3) unsigned default '0',
usagestate tinyint(3) unsigned default '0',
alarmstate tinyint(3) unsigned default '0',
unknownstate tinyint(3) unsigned default '0',
attrdatlen int(10) unsigned default '0',
attrdat blob,
modidate timestamp NOT NULL,
PRIMARY KEY (instanceid),
KEY i_moinfo (parentInstanceid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
删除语句:
DELETE FROM moinfo
插入语句:
INSERT INTO moinfo(instanceid,parentInstanceid,\
dn,mocid,flag ,fid,attrdatlen,attrdat)values(?,?,?,?,?,?,?,?)
查询语句1(包括将结果写入文件42280254字节):
SELECT INSTANCEID,PARENTINSTANCEID,DN,MOCID,FLAG,FID,\
ATTRDATLEN,length(attrdat) AS ATTRDATLEN2,ATTRDAT \
FROM MOINFO WHERE PARENTINSTANCEID= ? ")
查询语句2:
SELECT COUNT(1) FROM moinfo;
对于10000条记录(每条记录4228字节)MySQL的处理时间(单位 秒)如下:
测试序号
删除语句
插入语句
查询语句1
查询语句2
1
117″
476″
37″
6.80″
2
114″
479″
30″
6.69″
3
114″
475″
29″
6.70″
4
117″
476″
30″
6.72″
5
113″
474″
30″
6.68″
1.2.C++ API
MySQL的C++开放包是基于标准模板函数库STL的MySQL接口工具。目前版本是1.7.9,由于我们使用的是VC只有1.7.1版本可以使用。C++API由于使用了STL容器所以编程较C API来得方便。
1.2.1. 如何执行数据定义语句使用Query类可以方便地进行所有操作。例如:
Query query = connection.query(); // create a new query object
try { // ignore any errors here
// I hope to make this simpler soon
query.execute("drop table stock");
} catch (BadQuery er) {}
范例程序参见附录二。
1.2.2. 如何执行数据操作语句对指定的stock表插入三条记录,例如:
query << "insert into %5:table values (%0q, %1q, %2, %3, %4q)";
query.parse();
// set up the template query I will use to insert the data. The
// parse method call is important as it is what lets the query
// know that this is a template and not a literal string
query.def["table"] = "stock";
// This is setting the parameter named table to stock.
query.execute ("Hamburger Buns", 56, 1.25, 1.1, "1998-04-26");
query.execute ("Hotdogs' Buns" ,65, 1.1 , 1.1, "1998-04-23");
query.execute ("Dinner Roles" , 75, .95, .97, "1998-05-25");
query.execute ("Allen Lee" , 87, 1.5, 1.75, "1998-09-04");
范例程序参见附录二。
1.2.3. 如何执行数据库动态操作对输出和输入参数变化的SQL语句进行处理,例如:
Query query = con.query();
query << "select * from stock";
Result res = query.store();
cout << "Query: " << query.preview() << endl;
cout << "Records Found: " << res.size() << endl << endl;
cout << "Query Info:\n";
for (unsigned int i = 0; i < res.names().size(); i++) {
cout << setw(2) << i
<< setw(15) << res.names(i).c_str()
// this is the name of the field
<< setw(15) << res.types(i).sql_name()
// this is the SQL identifier name
// Result::types(unsigned int) returns a mysql_type_info which in many
// ways is like type_info except that it has additional sql type
// information in it. (with one of the methods being sql_name())
<< setw(20) << res.types(i).name()
// this is the C++ identifier name which most closely resembles
// the sql name (its is implementation defined and often not very readable)
<< endl;
}
范例程序参见附录三。
2. 其它说明 MySQL提供类似sqlplus的客户端登录工具mysql,可以方便的如sqlplus一样spool查询结果放入文本文件中。它还提供数据导出和导入工具,可以方便的导出数据文本文件和导入文本文件。当然网上也有较多的MySQL数据库图形化管理工具。本文地址:http://com.8s8s.com/it/it25959.htm