MySQL 5 C API Access Database Example

xiaoxiao2021-04-09  352

/ ************************************************** **************** The following is the result of studying the results derived by MySQL 5.0, describing and using standard C to demonstrate the flow of simply operational databases using the MySQL C API function; example program in VC6 Windows 2000 debugging through ***************************************************************** ***************************** / # include #include #include // file is located at the c API directory provided by MySQL ( Include) Using Namespace STD;

Add -LMysql -i / usr / local / mysql / inLuCDE # Pragma Comment (Lib, "Libmysql.lib") in // Linux and so on.

class CMysqlConnect {private: // connected MYSQL * m_connmysql; std :: string m_err; protected: // public: CMysqlConnect (const std :: string & host, const std :: string & user, const std :: string & password, const std :: string & dbname, unsigned int port); ~ cmysqlconnect (); // bool // Get the connection mysql * getConnect ();

// Get the error message const st :: string what ();

CMysqlConnect :: CMysqlConnect (const std :: string & host, const std :: string & user, const std :: string & password, const std :: string & dbname, unsigned int port = MYSQL_PORT): m_connmysql (NULL) {// initialize data structures IF ((m_connmysql = mysql_init (m_connmysql)) == null) {return;}

IF (null == mysql_real_connect (m_connmysql, host.c_str (), user.c_str (), password.c_str (), dbname.c_str (), port, null, 0)) {m_ERR = mysql_error (m_connmysql); return; }

CMYSQLCONNECT :: ~ cmysqlconnect () {if (m_connmysql) {mysql_close (m_connmysql); m_connmysql = null;}}

// Get the connection mysql * cmysqlconnect :: getConnect () {i (m_connmysql == null) {// throw std :: logic_error ("db not connection");}

// Need to determine if it can be automatically reconnected

Return M_Connmysql;}

// Get the error message const st :: string cmysqlconnect :: What () {return m_ERR;}

/ ************************************************** **************** / //// name: main // function: main test function // access: public // para: // 1. Int argc //: system Number of parameters // 2.: Char * argv [] //: Parameter value // Return: Return to the STARTUP function exit parameters // Author: hzh // Date: 2006-06-24 / ****** *********************************************************** ********* / int Main (int Argc, char * argv []) {cmysqlconnect * myconn = null; // Connect database IF (argc == 1) {MyConn = new cmysqlconnect ("127.0.0.1 "," root "," mysql5 "," test ", mysql_port); if (myconn == null) {std :: cout <<" allocate memory exception << endl; return -1;} if (MyConn-> GetConnect () == NULL) {std :: cout << "Connect Database Fail" << Endl << myconn-> what (). C_str () << endl; return -1;}} else if (argc == 6) {MyConn = New CMYSQLCONNECT (Argv [1], Argv [2], Argv [3], Argv [4], ATOI (Argv [5])); if (myconn == null) {std :: cout < <"Allocate Memory Exception" << Endl; Return-1;} if (null == myconn-> getConnect () ) {Std :: cout << "Connect Database Fail" << Endl << myconn-> what (). C_str () << ENDL; RETURN-1;}} else {std :: cout << "Run Parameter Error "<< Endl; return -1;} mysql * mydata = myconn-> getConnect ();

// First delete the data table std :: string s_sql = "drop table hzhtest"; if (MySQL_QUERY (MyData, S_SQL.C_STR ())! = 0) {// Delete Table Failure std :: cout << "Drop Table Fail "<< Endl << mysql_error (mydata) << endl;} else {std :: cout <<" Drop Table success "<< Endl;}

// Create a data sheet, the field MyId sets the self-printed attribute s_sql = "Create Table Hztest ("; s_sql = "Myid Integer Not Null Auto_Increment,"; s_sql = "MyTime DateTime Null, MyName Varchar (30)," ; S_sql = "Primary Key (MyID))"; if (MySQL_Query (MyData, s_sql.c_str ())! = 0) {// Create a table Failed std :: cout << "create table fail" << ENDL; Return -1;} else {std :: cout << "create table success" << endl; std :: cout << s_sql.c_str () << ENDL;} // Insert Data for the table (INT K = 1; K <30; k) {s_sql = "INSERT INTO HZHTEST (MyTime, MyName) VALUES"; s_sql = "('2006-06-"; char buff [20]; MEMSET (buff, 0, sizeof (buff)); ITOA (K, BUFF, 10); S_SQL = BUFF; S_SQL = ""

INT i = k% 3; MEMSET (BUFF, 0, SIZEOF (BUFF)); ITOA (I, BUFF, 10); S_SQL = BUFF; S_SQL = ": 01: 01 '";

IF (i == 0) {s_sql = ", null";} else {s_sql = ", 'Chinese display test"; s_sql = buff; s_sql = "";} s_sql = ")"

IF (MyData, S_Sql.c_str ())! = 0) {// Execute SQL Statement error std :: cout << "Execute Insert Syntax Fail" << Endl; Return -1;}} std :: cout < <"INSERT DATA SUCCESS << ENDL;

// Query the data and display s_sql = "select myid, mytime, myname from hztest"; if (mysql_query (mydata, s_sql.c_str ())! = 0) {// Execute SQL statement error RETURN-1;} mysql_res * Result = mysql_store_result (myData);

// get the result of the query int ROWCOUNT = mysql_num_rows (result); // Get effective record number std :: cout << "EXEC SQL:" << s_sql.c_str () << ", row count: << RowCount << ENDL;

MySQL_Field * fields = null; // acquire each field name for (int i = 0; fields = mysql_fetch_field (result); i) {std :: cout << fields-> name << "/ t / t"; } Std :: cout << Endl; // read each record mysql_row currrow = null; while ((currrow = mysql_fetch_row (result))! = Null) {// read the record for (int i = 0; I

转载请注明原文地址:https://www.9cbs.com/read-133142.html

New Post(0)