linux 下C++连接mysql数据库

本文介绍了一个使用C++在Linux环境下连接MySQL数据库的方法。通过一个简单的类`CMyDB`实现了数据库的连接、SQL语句的执行及表的创建等功能,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mjrao

linux 下C++连接mysql数据库

mjrao 发布于 2013年01月07日 19时, 0评/814阅
分享到 
收藏 +12
0
linux 下C++连接mysql数据库
标签: <无>

代码片段(3)

[文件] mydb.h ~ 416B    下载(37)

01#ifndef __MY_DB_H
02 
03#include <mysql/mysql.h>
04#include <stdlib.h>
05#include <stdio.h>
06#include <iostream>
07#include <string.h>
08using namespace std;
09class CMyDB
10{
11public:
12    CMyDB();
13    bool initDB(string server_host , string user, string password, string db_name);
14    bool executeSQL(string sql_str);
15    bool create_table(string table_str_sql);
16    ~CMyDB();
17private:
18    MYSQL *connection;
19    MYSQL_RES *res;
20    MYSQL_ROW row;
21};
22#endif

[文件] mydb.cpp ~ 2KB    下载(26)

01#include "mydb.h"
02 
03CMyDB::CMyDB()
04{  
05    //初始化连接数据库变量
06    connection = mysql_init(NULL);
07    if(connection == NULL)
08    {
09        perror("mysql_init");
10        exit(1);
11    }
12}
13CMyDB::~CMyDB()
14{  
15    //关闭初始化连接数据库变量
16    if(connection != NULL)
17    {
18        mysql_close(connection);
19    }
20}
21//初始化数据库 数据库连接
22bool CMyDB::initDB(string server_host , string user, string password , string db_name )
23{  
24    //运用mysql_real_connect函数实现数据库的连接
25    connection = mysql_real_connect(connection , server_host.c_str() , user.c_str() , password.c_str() , db_name.c_str() , 0 , NULL , 0);
26    if(connection == NULL)
27    {
28        perror("mysql_real_connect");
29        exit(1);
30    }
31    return true;
32}
33//执行SQL语句
34bool CMyDB::executeSQL(string sql_str)
35{
36    // 查询编码设定
37    if(mysql_query(connection, "set names utf8"))
38    {
39        fprintf(stderr, "%d: %s\n",mysql_errno(connection), mysql_error(connection));
40    }
41    int t = mysql_query(connection,  sql_str.c_str());
42    if(t)
43    {
44        printf("Error making query: %s\n" , mysql_error(connection));
45        exit(1);
46    }
47    else
48    {  
49        //初始化逐行的结果集检索
50        res = mysql_use_result(connection);
51        if(res)
52        {
53            //mysql_field_count(connection)   返回作用在连接上的最近查询的列数
54            for(int i = 0 ; i < mysql_field_count(connection) ; i++)
55            {  
56                //检索一个结果集合的下一行
57                row = mysql_fetch_row(res);    
58                if(row <= 0)
59                {
60                    break;
61                }
62                //mysql_num_fields(res)  函数返回结果集中字段的数
63                for(int r = 0 ; r  < mysql_num_fields(res) ; r ++)
64                {
65                    printf("%s\t" , row[r]);   
66                }
67                printf("\n");
68            }
69        }
70        //释放结果集使用的内存
71        mysql_free_result(res);
72    }
73    return true;
74}
75//表的创建
76bool CMyDB::create_table(string table_str_sql)
77{
78    int t = mysql_query(connection , table_str_sql.c_str());
79    if(t)
80    {
81        printf("Error making query: %s\n" , mysql_error(connection));
82        exit(1);
83    }
84    return true;
85}

[文件] main.cpp ~ 370B    下载(27)

01#include "mydb.h"
02int main(int argc,char **argv)
03{
04    CMyDB my;
05    my.initDB("localhost" , "root", "123456" , "studentInfo" );
06    my.executeSQL("select * from student;");
07    return 0;
08}
09 
10/*
11* sql语句
12*create database studentInfo
13*use studentInfo
14*create table student(id nchar(10) not null unique, name nchar(15) , sex nchar(4) check(sex in('男' , '女')), age nchar(3));
15*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值