服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++用mysql自带的头文件连接数据库

C++用mysql自带的头文件连接数据库

2021-04-10 13:39C++教程网 C/C++

现在正做一个接口,通过不同的连接字符串操作不同的数据库。要用到mysql数据库。通过网上的一些资料和自己的摸索,大致清楚了C++连接mysql的方法。可以通过2种方法实现。第一种方法是利用ADO连接,第二种方法是利用mysql自己的

mysql.h文件在哪,怎么查找。自行百度

?
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <mysql/mysql.h>
#include <stdio.h>
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
MYSQL *conn;
 MYSQL_RES *res;
 MYSQL_ROW row;
 
class people
{
public:
  char name[20];
  int pid;
  int type;
  char phone[30];
public:
  people(int a){};
  people(){
    setall();
  };
  ~people(){};
public:
  void setall();
 
};
 
void people::setall()
{
  cout<<"请输入该用户的编号"<<endl;
  cin>>pid;
  cout<<"请输入该用户的名字"<<endl;
// gets(name);
  cin>>name;
  cout<<"请输入该用户的类型"<<endl;
  cin>>type;
  cout<<"请输入该用户的联系方式"<<endl;
  cin>>phone;
 
}
void save()
{
  char sql[1000];
  people a;
  sprintf(sql,"insert into student values(%d,'%s',%d,'%s')",a.pid,a.name,a.type,a.phone);
  if(mysql_query(conn, sql))
    {
      printf("添加失败: (%s)\n",mysql_error(conn));
      return;
    }
    else
    {
      printf("添加成功!\n");
      return;
    }
  return;
}
void update(){
  char sql[1000];
  people a(1);
  cout<<"请输入你要更改的用户的编号:";
  cin >> a.pid;
  cout<<"请输入你要此编号用户的姓名:";
  cin >> a.name;
  cout <<"请输入你要更改的用户的类型:";
  cin >> a.type;
  cout << "请输入你要更改的用户的电话:";
  cin >> a.phone;
  sprintf(sql,"update student set name = '%s',usetype=%d,phone='%s' where pid = %d",a.name,a.type,a.phone,a.pid);
    if(mysql_query(conn, sql))
      {
        printf("更改失败: (%s)\n",mysql_error(conn));
        return;
      }
      else
      {
        printf("更改成功!\n");
        return;
      }
    return;
}
void del()
{
  char sql[1000];
  int pid;
  cout<<"请输入你要删除的人的编号"<<endl;
  cin>>pid;
  sprintf(sql,"delete from student where pid = %d",pid);
  if(mysql_query(conn, sql))
    {
      printf("删除 失败(%s)\n",mysql_error(conn));
      return;
    }
    else
    {
      printf("删除成功!\n");
      return;
    }
  return;
}
void menu()
{
  cout<<"1.用户录入"<<endl;
  cout<<"2.显示"<<endl;
  cout<<"3.更改"<<endl;
  cout<<"4.删除"<<endl;
  cout<<"5.退出"<<endl;
}
void show()
{
   if (mysql_query(conn, "select * from student")) {
     fprintf(stderr, "%s\n", mysql_error(conn));
     return;
    }
    res = mysql_use_result(conn);
 
    printf("编号\t名字\t类型\t联系方式\n");
    while ((row = mysql_fetch_row(res)) != NULL){
     cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<endl;
    }
    mysql_free_result(res);
 
}
int main() {
  int s;
  conn = mysql_init(NULL);
  if (!mysql_real_connect(conn, "localhost",
     "root", "root", "abc", 0, NULL, 0)) {
   fprintf(stderr, "%s\n", mysql_error(conn));
   return -1;
  }
  mysql_query(conn,"set names utf8");
  while(true){
    menu();
    cin>>s;
    if(s==2){show();}
    if(s==1){save();}
    if(s==3){update();}
    if(s==4){del();}
    if(s==5){mysql_close(conn);return 0;}
    cout<<"按任意键继续.."<<endl;
    getchar();
  }
  return 0;
}

延伸 · 阅读

精彩推荐