c的mysql例子

更多c的mysqlAPI见
https://blog.csdn.net/weibo1230123/article/details/81670778

 编译的时候要带上**-lmysqlclient**

#include 
#include 
using namespace std;
int main()
{
    const char* db = "yourdb";
    const char* server = "localhost";
    const char* user = "root";
    const char* password = "******";
    MYSQL *con = NULL;
    //初始化连接
    con = mysql_init(con);
    //建立连接
    con = mysql_real_connect(con, server, user, password, db, 3306, NULL, 0);
    //执行查询语句
    if (mysql_query(con, "SELECT username,password FROM user"))
        cout << "error\n";
    //获取结果集
    MYSQL_RES *result = mysql_store_result(con);
    //获得下一行
    while (MYSQL_ROW row = mysql_fetch_row(result))
    {
        string temp1(row[0]);
        string temp2(row[1]);
        cout<< temp1 <<"  "<< temp2<< endl;
    }
    //关闭连接
    mysql_close(con);
    return 0;
}

你可能感兴趣的:(mysql必知必会,mysql)