#include <iostream> #include <mysql/mysql.h> using namespace std; int main() { MYSQL * mysql; mysql = mysql_init(nullptr); string name; string pwd; string database; cout << "请输入用户名" ; cin >> name; cout << "请输入密码"; cin >> pwd; cout << "请输入进入的Databases"; cin >> database; /*连接到数据库*/ if( !mysql_real_connect(mysql , "localhost" , name.c_str() , pwd.c_str(), database.c_str() , 0 , nullptr , 0) ) { cout << mysql_error(mysql) << endl; exit(-1); } string str = "select * from user"; /*对数据库使用命令*/ mysql_query(mysql , str.c_str()); /*获得一个结果*/ MYSQL_RES *res = mysql_store_result(mysql); MYSQL_ROW row; /*行数不为空*/ if(mysql_num_rows(res)) { /*自动偏移 offset*/ while( (row = mysql_fetch_row( res ) ) ) { /*行对应元祖中的第二个数据*/ cout << row[0] << " and " << row[1] << endl; } } mysql_close(mysql); return 0; }
第一次MYSQL C API 简单的连接程序
猜你喜欢
转载自blog.csdn.net/cxhmyself/article/details/79793775
今日推荐
周排行