C++ mysqlclient

1.Install mysqlclient

sudo apt-get install mysql-client

 

 

 

 2.Add #include <mysql/myql.h> and cpp file

#include <iostream>
#include <mysql/mysql.h>

using namespace std;

void mysqlClientDemo();

int main(int args, char **argv)
{
    mysqlClientDemo();
}

void mysqlClientDemo()
{
    MYSQL_RES *result;
    MYSQL_ROW row;
    MYSQL *conn, mysql;
    int state;

    mysql_init(&mysql);
    conn = mysql_real_connect(&mysql, "localhost", "root", "Password", "myDB", 0, 0, 0);

    if (conn == NULL)
    { 
        cout<<mysql_error(&mysql)<<endl; 
        return;
    }

    state = mysql_query(conn, "select * from mt limit 100;");
    if (state != 0)
    {
        cout<<mysql_error(conn)<<endl;
        return;
    }

    result = mysql_store_result(conn);
    cout<< mysql_num_rows(result)<<endl;
    while ((row = mysql_fetch_row(result)) != NULL)
    {
        cout<<"Index="<<row[0]<<",Id="<<row[1]<<",Name="<<row[2]<<",Title="<<row[3]<<endl;
    }
}

3.Compile

 g++ -g -std=c++2a -I. -Wall -I/usr/include/mysql/ *.cpp -o h1 -L/usr/lib -lmysqlclient;

4. Execute 

./h1 0

 

When I remove the limit 100 strictions and select *from table it will return all which illustrated its performance is awesome.

 

posted @ 2022-05-15 22:15  FredGrit  阅读(173)  评论(0编辑  收藏  举报