# Include "DBAccess. h"
# Include <iostream>
// Using namespace std;
DBAccess: DBAccess ()
{
}
DBAccess ::~ DBAccess ()
{
}
Bool DBAccess: MysqlConnect ()
{
Mysql_init (& mysql );
If (mysql_real_connect (& mysql, "localhost", "root", "123456", "DBTroFish", 3306, NULL, 0) // parameter: Host localhost, user name root, password root, database DBTroFish
{
Return true;
}
Else
{
Return false;
}
}
Bool DBAccess: ExecuteSQL (std: string SQL) // add, delete, modify, and delete data
{
If (! MysqlConnect ())
{
Cout <"connection failed! "<Mysql_error (& mysql) <endl; // used for testing
Mysql_close (& mysql); // effectively solves too transaction connection
Return false;
// Exit (1); // exit the entire program
}
If (mysql_query (& mysql, SQL. c_str () // return value: 0 indicates normal, and if it is not 0, an error occurs.
{
Cout <"this operation is incorrect! "<Mysql_error (& mysql) <endl;
Mysql_close (& mysql );
Return false;
}
If (mysql_affected_rows (& mysql)> 0) // return value:> 0 indicates the number of affected rows. = 0 indicates no result, and =-1 indicates an error.
{
Mysql_close (& MySQL );
Return true;
}
Else
{
Mysql_close (& MySQL );
Return false;
}
}
Mysql_res * dbaccess: getdataset (STD: String SQL) // multi-row data query operation
{
Mysqlconnect ();
If (mysql_query (& MySQL, SQL. c_str ()))
{
Cout <"This query error! Error Code: "<mysql_errno (& MySQL) <" cause: "<mysql_error (& MySQL) <Endl;
}
Result = mysql_store_result (& MySQL );
Mysql_close (& mysql );
// Mysql_free_result (result); // call this method only after use; otherwise, the memory will continue to grow until the program ends.
Return result;
}
MYSQL_ROW DBAccess: GetRow (std: string SQL) // query a single row of data
{
Result = GetDataSet (SQL );
If (mysql_num_rows (result ))
{
Row = mysql_fetch_row (result );
Mysql_free_result (result); // solve the problem of memory Growth
Return row;
}
Else
{
Return NULL;
}
}
String DBAccess: IntToString (int)
{
Char t [20];
Sprintf (t, "% d", );
Return t;
}
# Include <iostream>
# Include <string. h>
# Include <stdio. h>
# Include <iostream>
# Include <mysql/mysql. h>
# Include "DBAccess. h"
# Include <stdlib. h>
Using namespace std;
Int main ()
{
If (1)
{
DBAccess myDBAccess;
String SQL;
SQL
= "Insert into tblCoinDetail (CoinTime, Bet, BeginCredit, dEndCredit, GiftFlag) values ('2017-02-42 ', '1', 2011, 1 )";
If (myDBAccess. ExecuteSQL (SQL ))
Cout <"operation successful" <endl;
Else
Cout <"operation failed" <endl;
}
Else
{
DBAccess myDBAccess;
MYSQL_ROW row;
String
SQL =
"Select date (CoinTime), sum (BeginCredit), sum (EndCredit), sum (BeginCredit-EndCredit) from tblCoinDetail group by CoinTime ";
MYSQL_RES * result;
Result = myDBAccess. GetDataSet (SQL );
If (result! = NULL) // if (mysql_num_rows (result) // determines whether it is NULL
{
While (row = mysql_fetch_row (result )))
{
If (row [0] = NULL) // solve the NULL address problem
{
Row [0] = (char *) "2011-01-01 ";
}
If (row [2] = NULL)
{
Row [2] = (char *) "0 ";
}
Fprintf (stdout, "% s |/t % s/n", row [0], row [1], row [2], row [3]);
}
}}
}