MySQL client API wrapper and support class. More...
#include <CMySQL.h>
Public Member Functions | |
CMySQL () | |
This is the constructor for the CMySQL class. This class was designed to wrap the functions, and in some cases simplify the use of the mysql client api. | |
~CMySQL () | |
This is the destructor for the CMySQL class. | |
const char * | GetClientInfo () |
This routine returns the character string representing the MySQL library version. | |
BOOL | Connect (const char *host=NULL, const char *user=NULL, const char *passwd=NULL, const char *db=NULL, uint port=0, const char *usock=NULL, uint clflag=0) |
This routine attempts to connect to a MySQL server. | |
BOOL | Close (BOOL bInit) |
This routine returns the character string representing the MySQL library version. | |
BOOL | Init () |
This routine Initializes the mysql structure. | |
BOOL | SelectDB (const char *db) |
This routine Causes the database *db to be the default database. | |
const char * | Stat () |
This routine returns the character string representing the MySQL library version. | |
BOOL | Query (char *str) |
This routine executes an SQL query. Do not use this routine if the data is binary. Also do not include a simicolon or \g. | |
BOOL | RealQuery (char *str, uint len) |
This routine executes an SQL query. Do not use this routine if the data is binary. Also do not include a simicolon or \g. | |
BOOL | CreateDatabase (const char *db) |
This routine creates a database for the current user. | |
BOOL | DropDatabase (const char *db) |
This routine deletes a database for the current user. | |
int | GetAffectedRows () |
This routine returns the number of affected rows (changed) by the the last 'UPDATE', 'DELETE', or 'INSERT' statements. For the 'SELECT' statement it returns the number of rows in the table. | |
MYSQL_RES * | StoreResult () |
You must call StoreResult() or UseResult() for every query which successfully retrieves data ('SELECT', 'SHOW', 'DESCRIBE', 'EXPLAIN'). | |
MYSQL_RES * | UseResult () |
You must call StoreResult() or UseResult() for every query which successfully retrieves data ('SELECT', 'SHOW', 'DESCRIBE', 'EXPLAIN'). | |
MYSQL_ROW | FetchRow (MYSQL_RES *res) |
Retrieves the next row from the result set. | |
void | FreeResult (MYSQL_RES *res) |
Frees the memory allocated for a result set by StoreResult(), UseResult(), ListDBs(), etc,. | |
int | NumRows (MYSQL_RES *res) |
This function returns the number of rows in the result set. | |
int | NumFields (MYSQL_RES *res) |
Get number of fields in result. | |
void | DataSeek (MYSQL_RES *result, ulong offset) |
This routine seeks to an arbitrary row in a query result set. | |
void | DataSeek (ulong offset) |
This routine seeks to an arbitrary row in a query result set. | |
const char * | GetError () |
Returns the error message for the most recently invoked MySQL function. | |
uint | GetErrno () |
Returns the error number for the most recently invoked MySQL function. | |
BOOL | CreateTable (const char *name, struct SFieldInfo **sinfo) |
This routine creates a table. | |
BOOL | CreateTable (struct STableInfo *table) |
This routine creates a table. | |
MYSQL_RES * | ListTables (const char *wild=NULL) |
This routine returns a result set consisting of table names in the current database that match the simple regular expression specified by the wild parameter. Wild may contain the wildcard characters '' or '_', or may be a NULL pointer to match all tables. Calling ListTables() is the same as executing the Query "SHOW ALL TABLES [LIKE WILD]". | |
BOOL | TableExists (const char *name) |
This routine checks to see if the table specified exists in the current database. | |
BOOL | TableExists (struct STableInfo *table) |
This routine checks to see if the table specified exists in the current database. | |
BOOL | DropTable (const char *name, BOOL bCascade=FALSE, BOOL bRestrict=FALSE) |
This routine deletes a table. | |
BOOL | DropTable (struct STableInfo *table, BOOL bCascade=FALSE, BOOL bRestrict=FALSE) |
This routine deletes a table. | |
BOOL | InsertRow (const char *name, struct SFieldInfo **sinfo, const char *Values) |
This routine inserts a row of data into the Table. | |
BOOL | InsertRow (struct STableInfo *table, const char *Values, int ValSize) |
This routine inserts a row of data into the Table. | |
long | GetTotalCount (struct STableInfo *table) |
This function queries the total number of records in a Table. |
Private Attributes | |
MYSQL | mysql |
This structure represents a handle to one database connection. It is used for almost all MySQL functions. | |
MYSQL_RES * | res |
This structure represents the result of a query that returns rows (SELECT, SHOW, DESCRIBE, EXPLAIN). The information returned from a query is called the result set in the remainder of this section. | |
BOOL | bConnected |
The class keeps track of whether or not we are currently connected to the Database for this instance of the class. | |
BOOL | bInitialized |
This is used to keep track to see if everything has been initialized. |
MySQL client API wrapper and support class.
CMySQL::CMySQL | ( | ) |
This is the constructor for the CMySQL class. This class was designed to wrap the functions, and in some cases simplify the use of the mysql client api.
CMySQL::~CMySQL | ( | ) |
This is the destructor for the CMySQL class.
This routine returns the character string representing the MySQL library version.
bInit | If True this routine will re-initialize the mysql structure after closing. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
BOOL CMySQL::Connect | ( | const char * | host = NULL , |
const char * | user = NULL , |
||
const char * | passwd = NULL , |
||
const char * | db = NULL , |
||
uint | port = 0 , |
||
const char * | usock = NULL , |
||
uint | clflag = 0 |
||
) |
This routine attempts to connect to a MySQL server.
host | Hostname or IP address of the Server, NULL means use localhost | ||||||||
user | Username, NULL means use the username that's logged in calling this program. | ||||||||
passwd | The password for user, don't encrypt this since the API will. | ||||||||
db | The database to use when the connection is made, NULL means use default db. | ||||||||
port | The port number used for the TCP/IP connection, 0 means to use default. | ||||||||
usock | unix_socket for a pipe connection. NULL is used for non-pipe connections. | ||||||||
clflag | client_flag is usually 0 but can be set to any of the following:
|
CR_CONN_HOST_ERROR | Failed to connect to the MySQL server. |
CR_CONNECTION_ERROR | Failed to connect to the local MySQL server. |
CR_IPSOCK_ERROR | Failed to create an IP socket. |
CR_OUT_OF_MEMORY | Out of memory. |
CR_SOCKET_CREATE_ERROR | Failed to create a Unix socket. |
CR_UNKNOWN_HOST | Failed to fine the IP address for the hostname. |
CR_VERSION_ERROR | A protocol mismatch resulted from attempting to connect to a server with a client library that uses a different protocol version. This can happen if you use a very old client library to connect to connect to a new server that wasn't started with the '–old-protocol' option. |
CR_NAMEDPIPEOPEN_ERROR | Failed to create a named pipe on Win32. |
CR_NAMEDPIPEWAIT_ERROR | Failed to wait for a named pipe on Win32. |
CR_NAMEDPIPESETSTATE_ERROR | Failed to get a pipe handler on Win32. |
BOOL CMySQL::CreateDatabase | ( | const char * | db | ) |
This routine creates a database for the current user.
db | The database to create. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
BOOL CMySQL::CreateTable | ( | const char * | name, |
struct SFieldInfo ** | sinfo | ||
) |
This routine creates a table.
name | The table name to create. |
sinfo | This is a pointer to the field information specifying each field that is used for this table. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
|
inline |
This routine creates a table.
table | The table structure that contains all the information needed to create the Table. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
void CMySQL::DataSeek | ( | MYSQL_RES * | result, |
ulong | offset | ||
) |
This routine seeks to an arbitrary row in a query result set.
result | Result handle returned by mysql_db_query() or mysql_query(). |
offset | This should be a value in the range from 0 to NumRows() - 1. |
|
inline |
This routine seeks to an arbitrary row in a query result set.
offset | This should be a value in the range from 0 to NumRows() - 1. |
BOOL CMySQL::DropDatabase | ( | const char * | db | ) |
This routine deletes a database for the current user.
db | The database to delete. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
This routine deletes a table.
name | The table to create. |
bCascade | Not used |
bRestrict | Not used |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
|
inline |
This routine deletes a table.
table | The table structure that contains all the information needed to drop the Table. |
bCascade | Not used |
bRestrict | Not used |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
MYSQL_ROW CMySQL::FetchRow | ( | MYSQL_RES * | res | ) |
Retrieves the next row from the result set.
res | Result handle returned by mysql_db_query() or mysql_query() |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
void CMySQL::FreeResult | ( | MYSQL_RES * | res | ) |
Frees the memory allocated for a result set by StoreResult(), UseResult(), ListDBs(), etc,.
res | Result handle returned by mysql_db_query() or mysql_query() |
int CMySQL::GetAffectedRows | ( | ) |
This routine returns the number of affected rows (changed) by the the last 'UPDATE', 'DELETE', or 'INSERT' statements. For the 'SELECT' statement it returns the number of rows in the table.
const char * CMySQL::GetClientInfo | ( | ) |
This routine returns the character string representing the MySQL library version.
|
inline |
Returns the error number for the most recently invoked MySQL function.
|
inline |
Returns the error message for the most recently invoked MySQL function.
long CMySQL::GetTotalCount | ( | struct STableInfo * | table | ) |
This function queries the total number of records in a Table.
table | This is the Table structure that contains all the details of the table we are querying. |
BOOL CMySQL::Init | ( | ) |
This routine Initializes the mysql structure.
BOOL CMySQL::InsertRow | ( | const char * | name, |
struct SFieldInfo ** | sinfo, | ||
const char * | Values | ||
) |
This routine inserts a row of data into the Table.
name | The table to insert the data into. |
sinfo | This is a pointer to the field information specifying each field that is used for this table. |
Values | This is the data that will be placed into this record. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
|
inline |
This routine inserts a row of data into the Table.
table | The table structure that contains all the information needed to perform this function. |
Values | This is the data that will be placed into this record. |
ValSize | Not used. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
MYSQL_RES * CMySQL::ListTables | ( | const char * | wild = NULL | ) |
This routine returns a result set consisting of table names in the current database that match the simple regular expression specified by the wild parameter. Wild may contain the wildcard characters '' or '_', or may be a NULL pointer to match all tables. Calling ListTables() is the same as executing the Query "SHOW ALL TABLES [LIKE WILD]".
wild | The wildcard characters for the search. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
int CMySQL::NumFields | ( | MYSQL_RES * | res | ) |
Get number of fields in result.
res | Result handle returned by mysql_db_query() or mysql_query(). |
int CMySQL::NumRows | ( | MYSQL_RES * | res | ) |
This function returns the number of rows in the result set.
res | Result handle returned by mysql_db_query() or mysql_query() |
BOOL CMySQL::Query | ( | char * | str | ) |
This routine executes an SQL query. Do not use this routine if the data is binary. Also do not include a simicolon or \g.
str | The query string to use. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
This routine executes an SQL query. Do not use this routine if the data is binary. Also do not include a simicolon or \g.
str | The query string to use. |
len | The length of the query string (allowing the use of binary data when required). |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
BOOL CMySQL::SelectDB | ( | const char * | db | ) |
This routine Causes the database *db to be the default database.
db | The database to use. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
const char * CMySQL::Stat | ( | ) |
This routine returns the character string representing the MySQL library version.
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
MYSQL_RES * CMySQL::StoreResult | ( | ) |
You must call StoreResult() or UseResult() for every query which successfully retrieves data ('SELECT', 'SHOW', 'DESCRIBE', 'EXPLAIN').
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
BOOL CMySQL::TableExists | ( | const char * | name | ) |
This routine checks to see if the table specified exists in the current database.
name | The table name to check against. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
|
inline |
This routine checks to see if the table specified exists in the current database.
table | The table structure that contains all the information needed to check if the Table exists. |
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
MYSQL_RES * CMySQL::UseResult | ( | ) |
You must call StoreResult() or UseResult() for every query which successfully retrieves data ('SELECT', 'SHOW', 'DESCRIBE', 'EXPLAIN').
CR_COMMANDS_OUT_OF_SYNC | Commands were executed out of sync. |
CR_SERVER_GONE_ERROR | The MySQL server has gone away. |
CR_SERVER_LOST | The connection to the server was lost during the Query. |
CR_UNKNOWN_ERROR | An unknown error occurred. |
|
private |
The class keeps track of whether or not we are currently connected to the Database for this instance of the class.
|
private |
This is used to keep track to see if everything has been initialized.
|
private |
This structure represents a handle to one database connection. It is used for almost all MySQL functions.
|
private |
This structure represents the result of a query that returns rows (SELECT, SHOW, DESCRIBE, EXPLAIN). The information returned from a query is called the result set in the remainder of this section.