CAPI connection to MySql bitsCN.com
Compiling Environment: WIN7, VS2010, MYSQL 5.0
Because the CRecordSet class that comes with VS2010 cannot match MYSQL5.0, it crashes when a CRecordSet class is created on my computer,
Therefore, if you want to use the ODBC interface, you can only directly call WINAPI, and the code is both ugly and troublesome.
Therefore, I wrote my own MYSQL class according to the MYSQL Manual. the debugging was successful. the source code is as follows:
Pse "> mysql. h
/*
* [3/9/2012]
* Powered by akaka
*
* Akaka_mysql_h
*
* C api connection to MYSQL database
* 1. include/include and/lib
* Ii. # include
(If this parameter is not added, compilation errors may occur)
* 3. add libmysql. lib
* 4. set environment variables: copy libmysql. dll in the/lib directory to the c:/windows/system32/directory.
*
*/
# Pragma once
# Include "std_lib_facilities.h"
# Include
# Include
Class CMysql {
Public:
CMysql ();
~ CMysql ();
Void connect (const char * host, const char * user, const char * passwd,
Const char * db, unsigned int port, const char * unix_socket, unsigned long client_flag );
Void query (const char * query );
Void printinfo ();
Void printrows ();
Private:
MYSQL * _ SQL;
MYSQL_RES * _ sqlres; // result set
Private:
Unsigned int _ column; // how many columns in a row in "result set"
};
Pse "> mysql. cpp
/*
* [3/9/2012]
* Powered by akaka
*
*
* Akaka_mysql.cpp
*/
# Include "akaka_mysql.h"
//------------------------------------------------------------------------------
CMysql: CMysql ()
{
If (_ SQL = mysql_init (NULL) = NULL)
{Throw runtime_error ("failed in mysql_init! ");}
}
CMysql ::~ CMysql ()
{
If (_ SQL)
Mysql_close (_ SQL );
}
//------------------------------------------------------------------------------
Void CMysql: connect (const char * host, const char * user, const char * passwd,
Const char * db, unsigned int port, const char * unix_socket, unsigned long client_flag)
{
If (mysql_real_connect (_ SQL, host, user, passwd, db,
Port, unix_socket, client_flag) = NULL)
Throw runtime_error ("failed in mysql_connect! ");
}
//------------------------------------------------------------------------------
Void CMysql: query (const char * query)
{
// Mysql_query Returns:
// Zero if the query was successful. Non-zero if an error occurred.
If (mysql_query (_ SQL, query ))
Throw runtime_error ("failed in mysql_query! ");
_ Sqlres = mysql_store_result (_ SQL); // Save the result set
}
//------------------------------------------------------------------------------
Void CMysql: printinfo ()
{
// Count how many columns in a row in "result set"
_ Column = 0;
MYSQL_FIELD * sqlfield;
While (sqlfield = mysql_fetch_field (_ sqlres) // traverses the field
{
Cout <'[' <sqlfield-> name <']';
_ Column ++;
}
Cout <endl;
}
//------------------------------------------------------------------------------
Void CMysql: printrows ()
{
MYSQL_ROW sqlrow;
While (sqlrow = mysql_fetch_row (_ sqlres) // traverses the result set
{
For (unsigned int I = 0; I <_ column; I ++) // traverse each column
{
Cout <'[' <sqlrow [I] <']';
}
Cout <endl;
}
}
Pse "> main. cpp
//------------------------------------------------------------------------------
// main
int main()
{
const char* host = "localhost";
const char* user = "root";
const char* passwd = "1121";
const char* db = "my";
const unsigned int port = 3306;
const char* unix_socket = NULL; // If unix_socket is not NULL, the string specifies the socket
// or named pipe that should be used.
// Note that the host parameter determines the type of the connection.
const unsigned long client_flag = 0; // The value of client_flag is usually 0, but can be set to a combination of the following flags to enable certain features:
try
{
CMysql mysql;
mysql.connect(host,user,passwd,db,port,unix_socket,client_flag);
mysql.query("SELECT * FROM myclass");
mysql.printinfo();
mysql.printrows();
return 0;
}catch(runtime_error& e)
{
cerr << e.what() << endl;
return 1;
}
}
Running result:
BitsCN.com