ubuntu c++ 连接mysql

来源:互联网 发布:iphoto下载 mac 编辑:程序博客网 时间:2024/06/05 07:39

C API的基本类型

MYSQL

    该结构代表1个数据库连接的句柄.    几乎所有的MySQL函数均使用它.    不应尝试拷贝MYSQL结构, 不保证这类拷贝结果会有用.

MYSQL_RES

    该结构代表返回行的查询结果

MYSQL_ROW

    这是1行数据的“类型安全”表示。它目前是按照计数字节字符串的数组实施的。    行是通过调用mysql_fetch_row()获得的。

1.得到指定数据库中的所有表

#include <iostream>#include <string>#include <vector>#include <mysql/mysql.h>using namespace std;int main(int argc, char* argv[]){     MYSQL mysql;//定义一个数据库连接句柄    mysql_init( &mysql );//对数据句柄进行初始化    mysql_real_connect(      &mysql,"localhost","root","root","test",3306,NULL,0);    //连接数据库(参数1,2不修改,参数3是数据库root一般不修改,参数4是数据库密码,参数5是访问mysql的datebase名字,后面参数可以不修改)    string sql = "show tables;";//查询数据库    mysql_query( &mysql, sql.c_str() );    MYSQL_RES *result = NULL;    result = mysql_store_result( &mysql );    //得到查询出来所有数据记录的数量    vector<string> allTable;    MYSQL_ROW row = mysql_fetch_row( result );    while( NULL != row )    {        allTable.push_back( row[0] );        row = mysql_fetch_row( result );    }    for(vector<string>::const_iterator cit = allTable.begin(); cit != allTable.end(); ++cit )    {        cout << *cit << "\t"; //每次查到打印名字    }    cout << endl;    mysql_free_result( result );    mysql_close( &mysql );    return 0;}

2.查询数据表的内容

#include <iostream>#include <mysql/mysql.h>using namespace std;int main(int argc, char* argv[]){    MYSQL mysql;    mysql_init( &mysql );    mysql_real_connect(        &mysql,"localhost","root","root","test",3306,        NULL,0      );    string sql = "select * from student";    mysql_query( &mysql, sql.c_str() );    MYSQL_RES *result = NULL;    result = mysql_store_result( &mysql );      //得到查询出来所有数据的条数    int row_count = mysql_num_rows( result );    cout << "all data number: " << row_count << endl;    //得到字段的个数和字段的名字    int field_count = mysql_num_fields( result );    cout << "field count : " << field_count << endl;    //得到所有字段的名字    MYSQL_FIELD* field = NULL;    for( int i = 0; i < field_count; ++i)    {        field = mysql_fetch_field_direct( result, i );        cout << field->name << "\t";    }    cout << endl;    //显示表中的所有数据    MYSQL_ROW row = NULL;    row = mysql_fetch_row( result );    while ( NULL != row )    {        for( int i = 0; i < field_count; ++i)        {            cout << row[i] << "\t";        }        cout << endl;        row = mysql_fetch_row( result );    }    mysql_free_result(result);    mysql_close( &mysql );    return 0;}

3.向表中插入(删除)一条数据

#include <iostream>#include <mysql/mysql.h>using namespace std;int main(int argc, char* argv[]){       MYSQL mysql;//准备mysql的访问结构    mysql_init( &mysql );    mysql_real_connect(        &mysql,"localhost","root","root","test",3306,        NULL,0     );    string sql = "insert into student value(1, 'jp', 24, 'gzjd')"; //插入    string sql = "delete from student where id = 33";//删除    mysql_query( &mysql, sql.c_str() );//执行sql语句    mysql_close( &mysql );//关闭数据库连接    return 0;}

4.更新表内容

#include <iostream>#include <mysql/mysql.h>using namespace std;int main(int argc, char* argv[]){    MYSQL mysql;    mysql_init( &mysql );    mysql_real_connect(        &mysql,"localhost","root","root","test",3306,        NULL,0     );    string sql = "update student set name = 'pj' where id = 2";    mysql_query( &mysql, sql.c_str() );    mysql_close(&mysql);    return 0;}

5.调用存储过程

#include <iostream>#include <mysql/mysql.h>using namespace std;int main(int argc, char* argv[]){    MYSQL mysql;    mysql_init( &mysql );    mysql_real_connect(        &mysql,"localhost","root","root","test",3306,        NULL,0     );     string sql = "call myPorc();";    int ret = mysql_query( &mysql, sql.c_str() );//  debug info//  cout << mysql_error( &mysql );//  cout << ret << endl;    mysql_close(&mysql);    return 0;}
原创粉丝点击