C ++ encapsulation class of MySQL

Source: Internet
Author: User
//. 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;
 
If (null = _ row)
Return nnullvalue; const char * filed = getstringfield (szfield); If (null = filed)
Return nnullvalue; return atoi (filed );
} Const char * cppmysqlquery: getstringfield (INT nfield, const char * sznullvalue/* = ""*/)
{
If (null = _ mysql_res)
Return sznullvalue; If (nfield + 1> (INT) _ field_count)
Return sznullvalue; If (null = _ row)
Return sznullvalue; return _ row [nfield];
}

Const char * cppmysqlquery: getstringfield (const char * szfield, const char * sznullvalue/* = ""*/)
{
If (null = _ mysql_res)
Return sznullvalue; int nfield = fieldindex (szfield );
If (nfield =-1)
Return sznullvalue;
 
Return getstringfield (nfield );
} Double cppmysqlquery: getfloatfield (INT nfield, double fnullvalue/* = 0.0 */)
{
Const char * field = getstringfield (nfield); If (null = field)
Return fnullvalue; return atol (field );
}
 
Double cppmysqlquery: getfloatfield (const char * szfield, double fnullvalue/* = 0.0 */)
{
Const char * field = getstringfield (szfield );
If (null = field)
Return fnullvalue; return atol (field );
} Void cppmysqlquery: nextrow ()
{
If (null = _ mysql_res)
Return; _ ROW = mysql_fetch_row (_ mysql_res );
} Bool cppmysqlquery: EOF ()
{
If (_ ROW = NULL)
Return true; return false;
} Cppmysqldb: cppmysqldb ()
{
_ Db_ptr = NULL;
} Cppmysqldb ::~ Cppmysqldb ()
{
If (_ db_ptr! = NULL)
{
Close ();
}
} Int cppmysqldb: open (const char * Host, const char * user, const char * passwd, const char * dB,
Unsigned int port/* = 0 */, unsigned long client_flag/* = 0 */)
{
Int ret =-1; _ db_ptr = mysql_init (null );
If (null = _ db_ptr)
Goto ext;
 
// If the connection fails, null is returned. For successful connections, the return value is the same as the value of the 1st parameter.
If (null = mysql_real_connect (_ db_ptr, host, user, passwd, DB, port, null, client_flag ))
Goto ext; // An error occurred while selecting the specified database.
// 0 indicates successful, and a non-0 value indicates an error.
If (mysql_select_db (_ db_ptr, DB )! = 0)
{
Mysql_close (_ db_ptr );
_ Db_ptr = NULL;
Goto ext;
} Ret = 0;
Ext:
// Failed to initialize MySQL Structure
If (ret =-1 & _ db_ptr! = NULL)
{
Mysql_close (_ db_ptr );
_ Db_ptr = NULL;
} Return ret;
} Void cppmysqldb: Close ()
{
If (_ db_ptr! = NULL)
{
Mysql_close (_ db_ptr );
_ Db_ptr = NULL;
}
} MySQL * cppmysqldb: getmysql ()
{
Return _ db_ptr;
}/* Process the query that returns multiple rows, and return the number of affected rows */
Cppmysqlquery & cppmysqldb: querysql (const char * SQL)
{
If (! Mysql_real_query (_ db_ptr, SQL, strlen (SQL )))
{
_ Db_query. _ mysql_res = mysql_store_result (_ db_ptr );
// _ Db_query. _ ROW = mysql_fetch_row (_ db_query. _ mysql_res );
// _ Db_query. _ row_count = mysql_num_rows (_ db_query. _ mysql_res );
//// Obtain the number of fields
// _ Db_query. _ field_count = mysql_num_fields (_ db_query. _ mysql_res );
} Return _ db_query;
}/* Execute a non-return result query */
Int cppmysqldb: execsql (const char * SQL)
{
If (! Mysql_real_query (_ db_ptr, SQL, strlen (SQL )))
{
// Obtain the number of affected rows
Return (INT) mysql_affected_rows (_ db_ptr );
}
Else
{
// Query execution failed
Return-1;
}
}/* Test whether the MySQL server is alive */
Int cppmysqldb: Ping ()
{
If (mysql_ping (_ db_ptr) = 0)
Return 0;
Else
Return-1;
}/* Shut down the MySQL server */
Int cppmysqldb: Shutdown ()
{
If (mysql_shutdown (_ db_ptr, shutdown_default) = 0)
Return 0;
Else
Return-1;
}/* Main function: restart the MySQL server */
Int cppmysqldb: reboot ()
{
If (! Mysql_reload (_ db_ptr ))
Return 0;
Else
Return-1;
}/*
* Description: transactions support InnoDB or bdb table types.
*/
/* Main function: Start transaction */
Int cppmysqldb: starttransaction ()
{
If (! Mysql_real_query (_ db_ptr, "Start transaction ",
(Unsigned long) strlen ("START transaction ")))
{
Return 0;
}
Else
// Query execution failed
Return-1;
}/* Main function: Submit a transaction */
Int cppmysqldb: Commit ()
{
If (! Mysql_real_query (_ db_ptr, "commit ",
(Unsigned long) strlen ("commit ")))
{
Return 0;
}
Else
// Query execution failed
Return-1;
}/* Main function: roll back the transaction */
Int cppmysqldb: rollback ()
{
If (! Mysql_real_query (_ db_ptr, "rollback ",
(Unsigned long) strlen ("rollback ")))
Return 0;
Else
// Query execution failed
Return-1;
}/* Get customer information */
Const char * cppmysqldb: getclientinfo ()
{
Return mysql_get_client_info ();
}/* Main function: Get the customer version information */
Const unsigned long cppmysqldb: getclientversion ()
{
Return mysql_get_client_version ();
}/* Main function: obtain host information */
Const char * cppmysqldb: gethostinfo ()
{
Return mysql_get_host_info (_ db_ptr );
}/* Main function: Get server information */
Const char * cppmysqldb: getserverinfo ()
{
Return mysql_get_server_info (_ db_ptr );}
/* Main function: Get server version information */
Const unsigned long cppmysqldb: getserverversion ()
{
Return mysql_get_server_version (_ db_ptr);}/* main function: Get the default Character Set of the current connection */
Const char * cppmysqldb: getcharactersetname ()
{
Return mysql_character_set_name (_ db_ptr);}/* Get system time */
Const char * cppmysqldb: getpolicime ()
{
// Return execquerygetsingvalue ("select now ()");
Return NULL;}/* Create a new database */
Int cppmysqldb: createdb (const char * name)
{
Char temp [1024]; sprintf (temp, "create database % s", name); If (! Mysql_real_query (_ db_ptr, temp, strlen (temp )))
Return 0;
 
Else
// Query execution failed
Return-1;
}/* Delete the specified database */
Int cppmysqldb: dropdb (const char * name)
{
Char temp [1024];
 
Sprintf (temp, "drop database % s", name); If (! Mysql_real_query (_ db_ptr, temp, strlen (temp )))
Return 0;
Else
// Query execution failed
Return-1;
} Bool cppmysqldb: tableexists (const char * table)
{
Return false;
} U_int cppmysqldb: lastrowid ()
{
Return 0;
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.