// header files
#include " mysql.h "
// function definition
// execute sql statements, including add, delete, update data
BOOL ExecuteSQL (MYSQL m_mysql, const char * sql)
{
IF (mysql_query (& m_mysql, sql))
{
// play error log, shown here directly to the console
cerr << " execute sql statement fails, the error message is: " << mysql_error (& m_mysql) << endl;
return false ;
}
the else
{
cout << " execute sql statement ! success " << endl;
}
return true;
}
// main function calls
int main()
{
MySQL MYSQL Based; // a database structure
A MYSQL_RES * RES; // a result set structure
MYSQL_ROW Row; // char ** two-dimensional array, storing a record
// initialize the database
mysql_init (& MySQL);
// set the encoding manner
to mysql_options (& MySQL, MYSQL_SET_CHARSET_NAME, " GBK " );
// connect to the database
// determines if the connection fails to output connection failure.
// Note the account name and password you connect the
IF (mysql_real_connect (& MySQL, " localhost " , " root " , "" , ", 3306 , NULL, 0 ) == NULL)
printf ( " Connection failed \ the n-! " );
/ Note the account name and password you connect
// query data
mysql_query (& MySQL, " the SELECT * from Chuang " );
// get results set
RES = mysql_store_result (& MySQL);
// increase the data test
Vector < String > VOS = Split (test_ming, " # " );
String ST = " INSERT INTO Chuang values ( ' " + VOS [ 0 ] + "', '" + vos[1] + "','" + str_time + "')";
const char* sql1 = st.c_str();
ExecuteSql(mysql, sql1);
//显示数据
while (row = mysql_fetch_row(res))
{
//row[0] = "七七七";
printf("%s", row[0]);//打印ID
printf("\t%s", row[1]);"
printf (Print the name//\ T% S \ n- " , Row [ 2 ]); // print salary
}
// release result set
mysql_free_result (RES);
// close the database
mysql_close (& MySQL);
return 0;
}