linux(ubuntu)下C++访问mysql数据库

  1. Ubuntu安装msyql

    1. 安装mysql数据库

      ?
      1
      sudo apt-get install mysql-server
    2. 安装mysql客户端

      ?
      1
      sudo apt-get install mysql-client
  2. C API的基本类型

    1. MYSQL

      1. 该结构代表1个数据库连接的句柄.

      2. 几乎所有的MySQL函数均使用它.

      3. 不应尝试拷贝MYSQL结构, 不保证这类拷贝结果会有用.

    2. MYSQL_RES

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

    3. MYSQL_ROW

      1. 这是1行数据的“类型安全”表示。它目前是按照计数字节字符串的数组实施的。

      2. 行是通过调用mysql_fetch_row()获得的。

    4. 官方文档地址 

      1. ?
        1
        http://dev.mysql.com/doc/refman/5.1/zh/apis.html#c
  3. 代码

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

      ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      #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,
               "192.168.16.114" ,   //要访问数据库的IP地址
               "root" ,         //用户名
               "root" ,         //密码
               "test" ,         //要访问的数据库
               3306,           //该数据库的端口
               NULL,           //一般为NULL
               0           //一般为0
           );
        
           //插入
           string sql = "insert into student value(1, 'jp', 24, 'gzjd')" ;
        
           //删除
           //string sql = "delete from student where id = 33";
        
           //执行sql语句
           mysql_query( &mysql, sql.c_str() );
       
           //关闭数据库连接
           mysql_close( &mysql );
       
           return 0;
      }
       
      //编译
           //g++ file.cpp -o target -lmysqlclient
       
      //执行
           //./target
       
      //验证
           //成功
    2. 更新表内容

      ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      #include <iostream>
      #include <mysql/mysql.h>
       
      using namespace std;
       
      int main( int argc, char * argv[])
      {
           MYSQL mysql;
           mysql_init( &mysql );
           
           mysql_real_connect(
               &mysql,
               "192.168.16.114" ,
               "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;
      }
    3. 调用存储过程

      ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      #include <iostream>
      #include <mysql/mysql.h>
       
      using namespace std;
       
      int main( int argc, char * argv[])
      {
           MYSQL mysql;
           mysql_init( &mysql );
           
           mysql_real_connect(
               &mysql,
               "192.168.16.114" ,
               "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;
      }
    4. 查询数据表的内容

      ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      #include <iostream>
      #include <mysql/mysql.h>
       
      using namespace std;
       
      int main( int argc, char * argv[])
      {
           MYSQL mysql;
           mysql_init( &mysql );
           mysql_real_connect(
               &mysql,
               "192.168.16.114" ,
               "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;
      }
    5. 得到指定数据库test中的所有表

      ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      #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,
               "192.168.16.114" ,
               "root" ,
               "root" ,
               "test" ,
               3306,
               NULL,
               0
           );
       
           //查询数据库
           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;
      }

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值