53 lines
1.5 KiB
C++
53 lines
1.5 KiB
C++
#include <iostream>
|
|
#include <mysql/mysql.h>
|
|
#include <string>
|
|
#include <unistd.h>
|
|
|
|
const std::string host = "47.100.131.185";
|
|
const std::string user = "jzh";
|
|
const std::string passwd = "a123";
|
|
const std::string db = "gogs";
|
|
const unsigned int port = 3306;
|
|
|
|
int main()
|
|
{
|
|
std::cout<<"mysql clientversion: "<<mysql_get_client_info()<<std::endl;
|
|
MYSQL *my = mysql_init(nullptr);
|
|
if (nullptr == my) {
|
|
std::cerr<<"init m=MySQL error"<<std::endl;
|
|
return 1;
|
|
}
|
|
std::cout<<my<<std::endl;
|
|
if (mysql_real_connect(my, host.c_str(), user.c_str(),passwd.c_str(),db.c_str(), port, nullptr, 0) == nullptr) {
|
|
std::cout<<"connect MySQL error"<<std::endl;
|
|
return 2;
|
|
}
|
|
//mysql_set_character_set(my, "utf8");
|
|
std::string cmd = "show tables";
|
|
int n = mysql_query(my, cmd.c_str());
|
|
if (n == 0) {
|
|
std::cout<<cmd<<" success"<<std::endl;
|
|
} else {
|
|
std::cout<<cmd<<" fail"<<std::endl;
|
|
}
|
|
MYSQL_RES *res = mysql_store_result(my);
|
|
if (res == nullptr) {
|
|
std::cout<<"mysql_store_result error"<<std::endl;
|
|
return 3;
|
|
}
|
|
int rows = mysql_num_rows(res);
|
|
int fields = mysql_num_fields(res);
|
|
std::cout<<"row = "<<rows<<std::endl;
|
|
std::cout<<"column = "<<fields<<std::endl;
|
|
for (int i = 0; i < rows; i++) {
|
|
MYSQL_ROW row = mysql_fetch_row(res);
|
|
for (int j = 0; j < fields; j++) {
|
|
std::cout<<row[j]<<"\t";
|
|
}
|
|
std::cout<<"\n";
|
|
}
|
|
mysql_free_result(res);
|
|
mysql_close(my);
|
|
return 0;
|
|
}
|