//. H /////////////////////////////////////// /////////////////////////////////////////
// Cppmysql-a c ++ wrapper around the MySQL database library.
//
// Copyright (c) 2009 Rob Groves. All Rights Reserved. lizp.net@gmail.com
//
// Permission to use, copy, modify, and distribute this software and its
// Documentation for any purpose, without failed, and without a written
// Agreement, is hereby granted, provided that the above copyright notice,
// This paragraph and the following two paragraphs appear in all copies,
// Modifications, and distributions.
//
// In no event shall the author be liable to any party for direct,
// Indirect, special, incidental, or consequential damages, including lost
// Profits, arising out of the use of this software and its documentation,
// Even if the author has been advised of the possibility of such damage.
//
// The author specifically disclaims any warranties, including, but not
// Limited to, the implied warranties of merchantability and fitness for
// Maid. The software and accompanying documentation, if
// Any, provided hereunder is provided "as is". The author has no obligation
// To provide maintenance, support, updates, enhancements, or modifications.
//
// U can use it for anything, but u must show the source
// Frome http://blog.csdn.net/bat603
// By Ben
// If u find some questions, please tell me with email
//
// V1.0 18/09/2009-initial version for cppmysql
//////////////////////////////////////// ////////////////////////////////////////
# Ifndef _ cpp_mysql_1_h _
# DEFINE _ cpp_mysql_1_h _ # include "mysql. H" typedef unsigned int u_int;
Typedef unsigned long u_long;
Typedef MySQL * db_handle; Class cppmysqldb; Class cppmysqlquery
{
Friend class cppmysqldb;
Public: cppmysqlquery (); // when the copy constructor is executed, the class in the brackets is no longer valid and cannot be used.
Cppmysqlquery (cppmysqlquery & rquery); // when the constructor is executed, = the class on the right is invalid and cannot be used again
Cppmysqlquery & operator = (cppmysqlquery & rquery); Virtual ~ Cppmysqlquery (); u_long numrow (); // number of rows
Int numfields (); // how many columns int fieldindex (const char * szfield );
// 0... n-1 Column
Const char * fieldname (INT ncol); // const char * fielddecltype (INT ncol );
// Int fielddatatype (INT ncol); u_long seekrow (u_long offerset); int getintfield (INT nfield, int nnullvalue = 0 );
Int getintfield (const char * szfield, int nnullvalue = 0); double getfloatfield (INT nfield, double fnullvalue = 0.0 );
Double getfloatfield (const char * szfield, double fnullvalue = 0.0); // 0... n-1 Column
Const char * getstringfield (INT nfield, const char * sznullvalue = "");
Const char * getstringfield (const char * szfield, const char * sznullvalue = ""); const unsigned char * getblobfield (INT nfield, Int & nlen );
Const unsigned char * getblobfield (const char * szfield, Int & nlen); bool fieldisnull (INT nfield );
Bool fieldisnull (const char * szfield); bool EOF (); void nextrow (); void finalize (); Private:
Void freeres ();
Void checkvm ();
PRIVATE:
Mysql_res * _ mysql_res;
Mysql_field * _ field;
Mysql_row _ row;
U_long _ row_count;
U_int _ field_count;}; Class cppmysqldb
{
Public: cppmysqldb (); Virtual ~ Cppmysqldb (); int open (const char * Host, const char * user, const char * passwd, const char * dB,
Unsigned int Port = 0, unsigned long client_flag = 0); void close ();/* return handle */
MySQL * getmysql ();/* process the query that returns multiple rows, and return the number of affected rows */
// The reference is returned because the Member Variable _ mysql_res must be left blank in the cppmysqlquery value assignment constructor.
Cppmysqlquery & querysql (const char * SQL );
/* Execute a non-return result query */
Int execsql (const char * SQL );
/* Test whether the MySQL server is alive */
Int Ping ();
/* Shut down the MySQL server */
Int Shutdown ();
/* Main function: restart the MySQL server */
Int reboot ();
/*
* Description: transactions support InnoDB or bdb table types.
*/
/* Main function: Start transaction */
Int starttransaction ();
/* Main function: Submit a transaction */
Int commit ();
/* Main function: roll back the transaction */
Int rollback ();
/* Obtain customer information */
Const char * getclientinfo ();
/* Main function: Get the customer version information */
Const unsigned long getclientversion ();
/* Main function: obtain host information */
Const char * gethostinfo ();
/* Main function: Get server information */
Const char * getserverinfo ();
/* Main function: Get server version information */
Const unsigned long getserverversion ();
/* Main function: Get the default Character Set of the current connection */
Const char * getcharactersetname ();
/* Obtain the system time */
Const char * getpolicime ();
/* Create a new database */
Int createdb (const char * Name );
/* Delete the specified database */
Int dropdb (const char * Name); bool tableexists (const char * Table); u_int lastrowid (); void setbusytimeout (INT nmillisecs ){};
PRIVATE: cppmysqldb (const cppmysqldb & dB );
Cppmysqldb & operator = (const cppmysqldb & dB); void checkdb (); Private:
/* Msyql connection handle */
MySQL * _ db_ptr;
Cppmysqlquery _ db_query;
}; # Endif //. CPP /////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////////////////////
// Cppmysql-a c ++ wrapper around the MySQL database library.
//
// Copyright (c) 2009 Rob Groves. All Rights Reserved. lizp.net@gmail.com
//
// Permission to use, copy, modify, and distribute this software and its
// Documentation for any purpose, without failed, and without a written
// Agreement, is hereby granted, provided that the above copyright notice,
// This paragraph and the following two paragraphs appear in all copies,
// Modifications, and distributions.
//
// In no event shall the author be liable to any party for direct,
// Indirect, special, incidental, or consequential damages, including lost
// Profits, arising out of the use of this software and its documentation,
// Even if the author has been advised of the possibility of such damage.
//
// The author specifically disclaims any warranties, including, but not
// Limited to, the implied warranties of merchantability and fitness for
// Maid. The software and accompanying documentation, if
// Any, provided hereunder is provided "as is". The author has no obligation
// To provide maintenance, support, updates, enhancements, or modifications.
//
// U can use it for anything, but u must show the source
// Frome http://rainfish.cublog.cn
// By Ben
//
// V1.0 18/09/2009-initial version for cppmysql
//////////////////////////////////////// //////////////////////////////////////// # Include "cppmysql. h"
# Include <string. h>
# Include <stdlib. h>
# Include <stdio. h> cppmysqlquery: cppmysqlquery ()
{
_ Mysql_res = NULL;
_ Field = NULL;
_ ROW = NULL;
_ Row_count = 0;
_ Field_count = 0;} cppmysqlquery: cppmysqlquery (cppmysqlquery & rquery)
{
* This = rquery;
} Cppmysqlquery & cppmysqlquery: Operator = (cppmysqlquery & rquery)
{
If (this = & rquery)
Return * This; _ mysql_res = rquery. _ mysql_res;
_ ROW = NULL;
_ Row_count = 0;
_ Field_count = 0;
_ Field = NULL; If (_ mysql_res! = NULL)
{
// Locate the cursor to the first position
Mysql_data_seek (_ mysql_res, 0 );
_ ROW = mysql_fetch_row (_ mysql_res );
_ Row_count = mysql_num_rows (_ mysql_res );
// Obtain the number of fields
_ Field_count = mysql_num_fields (_ mysql_res );
} Rquery. _ mysql_res = NULL;
Rquery. _ field = NULL;
Rquery. _ ROW = NULL;
Rquery. _ row_count = 0;
Rquery. _ field_count = 0; return * This;} cppmysqlquery ::~ Cppmysqlquery ()
{
Freeres ();
} Void cppmysqlquery: freeres ()
{
If (_ mysql_res! = NULL)
{
Mysql_free_result (_ mysql_res );
_ Mysql_res = NULL;
}
} U_long cppmysqlquery: numrow ()
{
Return _ row_count;
} Int cppmysqlquery: numfields ()
{
Return _ field_count;
} U_long cppmysqlquery: seekrow (u_long offerset)
{
If (offerset <0)
Offerset = 0;
If (offerset> = _ row_count)
Offerset = _ row_count-1; mysql_data_seek (_ mysql_res, offerset );
_ ROW = mysql_fetch_row (_ mysql_res );
Return offerset;
} Int cppmysqlquery: fieldindex (const char * szfield)
{
If (null = _ mysql_res)
Return-1;
If (null = szfield)
Return-1; mysql_field_seek (_ mysql_res, 0); // locate column 0th
U_int I = 0;
While (I <_ field_count)
{
_ Field = mysql_fetch_field (_ mysql_res );
If (_ field! = NULL & strcmp (_ field-> name, szfield) = 0) // locate
Return I; I ++;
} Return-1;
} Const char * cppmysqlquery: fieldname (INT ncol)
{
If (_ mysql_res = NULL)
Return NULL; mysql_field_seek (_ mysql_res, ncol );
_ Field = mysql_fetch_field (_ mysql_res); If (_ field! = NULL)
Return _ field-> name;
Else
Return NULL;
} Int cppmysqlquery: getintfield (INT nfield, int nnullvalue/* = 0 */)
{
If (null = _ mysql_res)
Return nnullvalue;
If (nfield + 1> (INT) _ field_count)
Return nnullvalue;
If (null = _ row)
Return nnullvalue;
Return atoi (_ row [nfield]);
} Int cppmysqlquery: getintfield (const char * szfield, int nnullvalue/* = 0 */)
{
If (null = _ mysql_res | null = szfield)
Return nnullvalue;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.