Chapter 21. Connectors and APIs http://dev.mysql.com/doc/refman/5.1/en/connectors-apis.html21.1. MySQL Connector/ODBC [+/-] 21.2. MySQL Connector/NET [+/-] 21.3. MySQL Connector/J [+/-] 21.4. MySQL Connector/MXJ [+/-] 21.5. MySQL Connector/C++ [+/-] 21.5.1. MySQL Connector/C++ Binary Installation 21.5.2. MySQL Connector/C++ Source Installation 21.5.3. MySQL Connector/C++ Building Windows applications with Microsoft Visual Studio 21.5.4. MySQL Connector/C++ Building Linux applications with NetBeans 21.5.5. MySQL Connector/C++ Getting Started: Usage Examples 21.5.6. MySQL Connector/C++ Tutorials 21.5.7. MySQL Connector/C++ Debug Tracing 21.5.8. MySQL Connector/C++ Usage Notes 21.5.9. MySQL Connector/C++ Known Bugs and Issues 21.5.10. MySQL Connector/C++ Feature requests 21.5.11. MySQL Connector/C++ Support 21.5.12. MySQL Connector/C++ FAQ 21.6. MySQL Connector/C [+/-] 21.6.1. Building MySQL Connector/C from the Source Code 21.6.2. Testing MySQL Connector/C 21.6.3. MySQL Connector/C FAQ 21.7. MySQL Connector/OpenOffice.org [+/-] 21.8. libmysqld, the Embedded MySQL Server Library [+/-] 21.9. MySQL C API [+/-] 21.9.1. C API Data Types 21.9.2. C API Function Overview 21.9.3. C API Function Descriptions 21.9.4. C API Prepared Statements 21.9.5. C API Prepared Statement Data types 21.9.6. C API Prepared Statement Function Overview 21.9.7. C API Prepared Statement Function Descriptions 21.9.8. C API Threaded Function Descriptions 21.9.9. C API Embedded Server Function Descriptions 21.9.10. Common Questions and Problems When Using the C API 21.9.11. Controlling Automatic Reconnection Behavior 21.9.12. C API Support for Multiple Statement Execution 21.9.13. C API Prepared Statement Problems 21.9.14. C API Prepared Statement Handling of Date and Time Values 21.9.15. C API Support for Prepared CALL Statements 21.9.16. Building Client Programs 21.10. MySQL PHP API [+/-] 21.11. MySQL Perl API 21.12. MySQL C++ API 21.13. MySQL Python API 21.14. MySQL Ruby APIs [+/-] 21.15. MySQL Tcl API 21.16. MySQL Eiffel Wrapper
MySQL提供了丰富的数据接口API,包括C、C++、Perl、PHP、Python、TCL等API和
JDBC,ODBC接口。出于性能考虑,我们采用了MySQL的C API进行开发。现以Visual C++
环境为例,作一简单介绍。
---- 新建一Win32 Console Application的Project,把“c:\mysql\include”添加到编
译选项的包含路径中(在Project Options中加入 /I "d:\mysql\include")。新建一m
ain.c文件,主体如下:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>
int main( int argc, char * argv[] )
{
char szTargetDSN[] = "test";
char szSqlText[500]="";
char aszFlds[ 25 ][ 25 ];
MYSQL * myData ;
MYSQL_RES * res ;
MYSQL_FIELD * fd ;
MYSQL_ROW row ;
int i,j,k;
BOOL bCreate = TRUE;
if ( (myData = mysql_init((MYSQL*) 0))
//初始化数据结构
&& mysql_real_connect( myData, NULL,
//连接数据库
"root", " your_password ", szTargetDSN,
MYSQL_PORT, NULL, 0 ) )
{
if(bCreate)
{
sprintf(szSqlText, //构造SQL语句
"create table mytable "
//新建一张表
"(time datetime, s1 char(6), "
"s2 char(11), s3 int, s4 int)");
if (mysql_query( myData, szSqlText))
//执行SQL语句
{//执行SQL语句出错
ErrLog( "Can't create table") ;
mysql_close( myData ) ;
return FALSE ;
}
}
sprintf(szSqlText,
"insert into mytable "
//向表中插入数据
"values('2000-3-10 21:01:30',"
//注意时间的格式
"'Test','MySQLTest',2000,3)");
if (mysql_query( myData, szSqlText))
{//执行SQL语句出错
ErrLog( "Can't insert data to table") ;
mysql_close( myData ) ;
return FALSE ;
}
sprintf(szSqlText, "select * from mytable ");
if (mysql_query( myData, szSqlText))
//进行数据检索
{
//执行SQL语句出错
mysql_close( myData ) ;
return FALSE ;
}
else
{
res = mysql_store_result( myData ) ;
//取得查询结果
i = (int) mysql_num_rows( res ) ;
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "user", "password");delete con;
21.6.5.2. MySQL Connector/C++ Running a simple querysql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmtdriver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "user", "password");stmt = con->createStatement();
stmt->execute("USE " EXAMPLE_DB);
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");
stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");delete stmt;
delete con;
Chapter 21. Connectors and APIs
http://dev.mysql.com/doc/refman/5.1/en/connectors-apis.html21.1. MySQL Connector/ODBC [+/-]
21.2. MySQL Connector/NET [+/-]
21.3. MySQL Connector/J [+/-]
21.4. MySQL Connector/MXJ [+/-]
21.5. MySQL Connector/C++ [+/-]
21.5.1. MySQL Connector/C++ Binary Installation
21.5.2. MySQL Connector/C++ Source Installation
21.5.3. MySQL Connector/C++ Building Windows applications with Microsoft Visual Studio
21.5.4. MySQL Connector/C++ Building Linux applications with NetBeans
21.5.5. MySQL Connector/C++ Getting Started: Usage Examples
21.5.6. MySQL Connector/C++ Tutorials
21.5.7. MySQL Connector/C++ Debug Tracing
21.5.8. MySQL Connector/C++ Usage Notes
21.5.9. MySQL Connector/C++ Known Bugs and Issues
21.5.10. MySQL Connector/C++ Feature requests
21.5.11. MySQL Connector/C++ Support
21.5.12. MySQL Connector/C++ FAQ
21.6. MySQL Connector/C [+/-]
21.6.1. Building MySQL Connector/C from the Source Code
21.6.2. Testing MySQL Connector/C
21.6.3. MySQL Connector/C FAQ
21.7. MySQL Connector/OpenOffice.org [+/-]
21.8. libmysqld, the Embedded MySQL Server Library [+/-]
21.9. MySQL C API [+/-]
21.9.1. C API Data Types
21.9.2. C API Function Overview
21.9.3. C API Function Descriptions
21.9.4. C API Prepared Statements
21.9.5. C API Prepared Statement Data types
21.9.6. C API Prepared Statement Function Overview
21.9.7. C API Prepared Statement Function Descriptions
21.9.8. C API Threaded Function Descriptions
21.9.9. C API Embedded Server Function Descriptions
21.9.10. Common Questions and Problems When Using the C API
21.9.11. Controlling Automatic Reconnection Behavior
21.9.12. C API Support for Multiple Statement Execution
21.9.13. C API Prepared Statement Problems
21.9.14. C API Prepared Statement Handling of Date and Time Values
21.9.15. C API Support for Prepared CALL Statements
21.9.16. Building Client Programs
21.10. MySQL PHP API [+/-]
21.11. MySQL Perl API
21.12. MySQL C++ API
21.13. MySQL Python API
21.14. MySQL Ruby APIs [+/-]
21.15. MySQL Tcl API
21.16. MySQL Eiffel Wrapper