金沙澳门唯一官网动用Mysql的亲力亲为步骤及种种

2019-11-08 08:33 来源:未知

目前一贯在上学C++下使用Mysql的方法及中间各类的难点,也看了不菲Mysql的API函数,当然本人看的依旧很功底的。其实对于各类数据库的操作,基本的点子都以极其周围的,好些个都是connect,select,update,delete以至insert这么些操作。接下来作者就将那多少个步骤以代码的章程展现给我们,相信应该能对我们对mysql的编制程序起到一个幼功模板的效果,方便大家从此以往对mysql数据库的打听和学习。

1.mysql的初叶化initConnection:

1)mysql_init():开首化数据库

2)mysql_real_connect()(不引入用Mysql_connect()):连接数据库

详尽代码如下:

 1 bool dataBase::initConnection() {
 2     try {
 3         if (mysql_init(&m_sqlCon) == NULL) {
 4             std::cout << "初始化数据库失败" << std::endl;
 5             return false;
 6         }
 7         // localhost:服务器 root为账号密码 test为数据库名 3306为端口    
 8         if (!mysql_real_connect(&m_sqlCon, host, user, pass,databaseName, port, NULL, 0))
 9         {
10             std::cout << "数据库连接失败" << std::endl;
11             return false;
12         }
13         std::cout << "数据库连接成功" << std::endl;
14         return true;
15     }
16     catch (...)
17     {
18         std::cout << "出错啦" << std::endl;
19         return false;
20     }
21 }

2.mysql的查询select:

1)mysql_query():实行询问语句

2)mysql_store_result():拿到查询结果

3)mysql_fetch_field()以及mysql_fetch_row():获得结果聚集的从头到尾的经过

4)mysql_free_result()

代码如下:

 1 void dataBase::user_query() {
 2     if (mysql_query(&m_sqlCon, "select * from user")) {
 3         std::cout << "查询失败" << std::endl;
 4         return;
 5     }
 6     std::cout << "查询成功" << std::endl;
 7     MYSQL_RES *result;
 8     result = mysql_store_result(&m_sqlCon);
 9     if (result) {
10         int row_num,col_num;
11         row_num = mysql_num_rows(result);
12         col_num = mysql_num_fields(result);
13         std::cout << "共有" << row_num << "条数据,以下为其详细内容:" << std::endl;
14         MYSQL_FIELD *fd;
15         while (fd = mysql_fetch_field(result)) {
16             std::cout << fd->name << "t";
17         }
18         std::cout << std::endl;
19         MYSQL_ROW sql_row;
20         while (sql_row = mysql_fetch_row(result)) {
21             for (int i = 0; i < col_num; i++) {
22                 if (sql_row[i] == NULL) std::cout << "NULLt";
23                 else std::cout << sql_row[i] << "t";
24             }
25             std::cout << std::endl;
26         }
27     }
28     if(result!=NULL)
29     mysql_free_result(result);
30 
31 }

3.mysql的插入语句insert:

金沙澳门唯一官网,1)mysql_query()通过select推断是或不是曾经存在(能够省略卡塔 尔(英语:State of Qatar)

2)通过mysql_query()实行扦插

代码如下:

 1 void dataBase::user_insert(UserBody &body) {
 2     char select_user[255];
 3     char query[255];
 4     MYSQL_RES *result;
 5     sprintf(select_user, "select * from user where UserName='%s'", body.userName);
 6     if (mysql_query(&m_sqlCon, select_user)||!(result = mysql_store_result(&m_sqlCon))) {
 7         std::cout << "插入查询失败" << std::endl;
 8         return;
 9     }
10     if (mysql_num_rows(result)) {
11         std::cout << "用户已存在" << std::endl;
12         mysql_free_result(result);
13         return;
14     }
15     mysql_free_result(result);
16     sprintf(query, "insert into user(UserName,UserPass,ConnectionNum,UseFor) values('%s','%s',%d,%d)", body.userName, body.userPass, body.connection, body.type);
17     if (mysql_query(&m_sqlCon, query)) {
18         std::cout << "插入数据失败" << std::endl;
19         return;
20     }
21     std::cout << "插入数据成功,共插入:" <<mysql_affected_rows(&m_sqlCon)<<"行" << std::endl;
22 }

4.mysql的改过语句update

校订与丰富的手续雷同,直接贴出代码就不做赘述了。

代码如下:

 1 void dataBase::user_update(UserBody &body) {
 2     char query[255];
 3     char select_user[255];
 4     MYSQL_RES *result;
 5     sprintf(select_user, "select * from user where UserName='%s'", body.userName);
 6     if (mysql_query(&m_sqlCon, select_user) || !(result = mysql_store_result(&m_sqlCon))) {
 7         std::cout << "修改查询失败" << std::endl;
 8         return;
 9     }
10     if (mysql_num_rows(result) == 0) {
11         std::cout << "要修改的用户不存在" << std::endl;
12         mysql_free_result(result);
13         return;
14     }
15     mysql_free_result(result);
16     sprintf(query, "update user set UserPass='%s',ConnectionNum=%d,UseFor=%d where UserName='%s'", body.userPass, body.connection, body.type, body.userName);
17     if (mysql_query(&m_sqlCon, query)) {
18         std::cout << "修改失败" << std::endl;
19         return;
20     }
21     std::cout << "修改成功,共修改:" << mysql_affected_rows(&m_sqlCon) << "行" << std::endl;
22 }

 

5.mysql的删除操作delete

事实上删除、订正和插入操作都以形似的,会个中八个别的相应都大概了,小编也就一贯贴出代码了。

代码如下:

 1 void dataBase::user_delete(char *userName) {
 2     char query[255];
 3     char select_user[255];
 4     MYSQL_RES *result;
 5     sprintf(select_user, "select * from user where UserName='%s'", userName);
 6     if (mysql_query(&m_sqlCon, select_user)||!(result = mysql_store_result(&m_sqlCon))) {
 7         std::cout << "删除查询失败" << std::endl;
 8         return;
 9     }
10     if (mysql_num_rows(result)==0) {
11         std::cout << "要删除的用户不存在" << std::endl;
12         mysql_free_result(result);
13         return;
14     }
15     mysql_free_result(result);
16     sprintf(query, "delete from user where UserName='%s'", userName);
17     if (mysql_query(&m_sqlCon, query)) {
18         std::cout << "删除失败" << std::endl;
19         return;
20     }
21     std::cout << "删除成功,共删除:" << mysql_affected_rows(&m_sqlCon) << "行" << std::endl;
22 }

 

以上算是对Mysql中常用的有个别代码的下结论,有啥样不全面包车型大巴希望大家能够提议来,也指望以此能在常常帮到大家,起到三个模板的职能,也能便宜大家对mysql的上学。笔者也归属叁个learner,也可望能和富贵人家在常常中并行学习。

转发请申明出处:。谢谢

 

TAG标签:
版权声明:本文由金沙澳门唯一官网发布于数据库管理,转载请注明出处:金沙澳门唯一官网动用Mysql的亲力亲为步骤及种种