没有合适的资源?快使用搜索试试~ 我知道了~
c++博客系统1
需积分: 0 1 下载量 131 浏览量
2022-08-03
15:08:06
上传
评论
收藏 604KB PDF 举报
温馨提示
试读
30页
第一节课: 介绍整体框架和业务流程, 设计数据库并建好数据表, 讲解使用 MySQL C API 操作数据库.第二节课: 根据 MySQL C API 封装数据
资源详情
资源评论
资源推荐
基于 C++ 的博客系统
重要知识点
1. 简单的Web服务器设计能力
2. C/C++ 操作 MySQL 数据库
3. 数据库设计
4. Restful 风格 API
5. json 和 jsoncpp 的使用
6. 强化 HTTP 协议的理解
7. cpp-httplib 的使用和原理
8. 正则表达式
9. Postman 工具的使用
10. boost 的使用
11. 前端页面的开发方法和技巧(免费模板 + bootstrap + Vue.js)
12. 使用 Editor.md Markdown编辑器
13. 软件测试的基本思想和方法
整体架构
博客服务器
1. 对博客的增删改查管理能力
2. 对标签的增删查改能力
博客客户端
1. 博客标题列表页面
2. 博客内容展示页面
3. 博客管理页面
4. 博客内容编辑页面
关于数据存储和交互
1. 服务器存储 markdown 格式的数据
2. 前端通过 editor.md 这个工具将markdown的文档内容提交给服务器, 或者解析服务器返回的 markdown 作为
前端页面内容.
授课预估
共需五节课左右.
第一节课: 介绍整体框架和业务流程, 设计数据库并建好数据表, 讲解使用 MySQL C API 操作数据库.
第二节课: 根据 MySQL C API 封装数据库操作, 实现服务器的数据存储层, 并进行测试. 介绍 cpp-httplib, 介绍正则表
达式. 设计服务器提供的 API.
第三节课: 介绍 jsoncpp. 基于 cpp-httplib 实现实现服务器 API, 并使用 Postman 测试.
第四节课: 简要介绍前端技术, 使用 免费模板 + Vue 构建出一个博客列表页面.
第五节课: 介绍 editor.md, 实现博客展示页面和博客编辑页面.
数据库设计
创建数据库
创建博客表
创建标签表
使用 MySQL C API 操作数据库
插入数据库
create database if not exists blog_system;
use blog_system;
drop table if exists `blog_table`
create table `blog_table`(blog_id int not null primary key auto_increment,
title varchar(50),
content text,
tag_id int,
create_time varchar(50))
drop table if exists `tag_table`
create table `tag_table`(tag_id int not null primary key auto_increment,
tag_name varchar(50))
///////////////////////////////////////////////////////
// 编译选项
// -L /usr/lib64/mysql -lmysqlclient
// 删除, 更新 和插入同理, 只是拼装成不同的 SQL 而已
///////////////////////////////////////////////////////
#include <cstdio>
#include <cstdlib>
#include <mysql/mysql.h>
int main() {
// 1. 初始化句柄
MYSQL* connect_fd = mysql_init(NULL);
// 2. 建立链接
// mysql_init 返回的指针
// 主机地址
// 用户名
// 密码
// 数据库名
查找数据库
mysql_select.cc
// 端口号
// unix_socket
// client_flag
if (mysql_real_connect(connect_fd, "127.0.0.1", "root", "",
"blog_system", 3306, NULL, 0) == NULL) {
printf("连接失败! %s\n", mysql_error(connect_fd));
return 1;
}
// 3. 设置编码格式
mysql_set_character_set(connect_fd, "utf8");
// 4. 拼装 SQL 语句
char sql[1024 * 4] = {0};
char title[] = "立一个 flag";
char content[] = "我要拿 30w 年薪";
int tag_id = 2;
char datetime[] = "2019/05/14 12:00";
sprintf(sql, "insert into blog_table values(null, '%s', '%s', %d, '%s')",
title, content, tag_id, datetime);
// 5. 执行 SQL 语句
int ret = mysql_query(connect_fd, sql);
if (ret < 0) {
printf("执行 sql 失败! %s\n", mysql_error(connect_fd));
return 1;
}
// 6. 关闭句柄
mysql_close(connect_fd);
printf("执行成功!\n");
return 0;
}
///////////////////////////////////////////////////////
// 编译选项
// -L /usr/lib64/mysql -lmysqlclient
// 删除, 更新 和插入同理, 只是拼装成不同的 SQL 而已
///////////////////////////////////////////////////////
#include <cstdio>
#include <cstdlib>
#include <mysql/mysql.h>
int main() {
// 1. 初始化句柄
MYSQL* connect_fd = mysql_init(NULL);
// 2. 建立链接
// mysql_init 返回的指针
// 主机地址
// 用户名
// 密码
// 数据库名
服务器 API 设计
博客管理 API 设计
新增博客
// 端口号
// unix_socket
// client_flag
if (mysql_real_connect(connect_fd, "127.0.0.1", "root", "",
"blog_system", 3306, NULL, 0) == NULL) {
printf("连接失败! %s\n", mysql_error(connect_fd));
return 1;
}
// 3. 设置编码格式
mysql_set_character_set(connect_fd, "utf8");
// 4. 拼装 SQL 语句
char sql[1024 * 4] = {0};
sprintf(sql, "select * from blog_table");
// 5. 执行 SQL 语句
int ret = mysql_query(connect_fd, sql);
if (ret < 0) {
printf("执行 sql 失败! %s\n", mysql_error(connect_fd));
return 1;
}
// 6. 遍历查询结果
MYSQL_RES* result = mysql_store_result(connect_fd);
if (result == NULL) {
printf("获取结果失败! %s\n", mysql_error(connect_fd));
return 1;
}
// a) 获取行数和列数
int rows = mysql_num_rows(result);
int fields = mysql_num_fields(result);
printf("rows: %d, fields: %d\n", rows, fields);
// b) 打印结果
for (int i = 0; i < rows; ++i) {
MYSQL_ROW row = mysql_fetch_row(result);
for (int j = 0; j < fields; ++j) {
printf("%s\t", row[j]);
}
printf("\n");
}
// 7. 释放结果集
mysql_free_result(result);
// 8. 关闭句柄
mysql_close(connect_fd);
printf("执行成功!\n");
return 0;
}
请求:
获取所有博客标题
删除博客
修改博客
POST /blog
{
"title": "我的第一篇博客",
"content": "博客的 markdown 格式内容",
"tag_id": 1,
"create_time": "2019/05/14 12:00",
}
响应:
HTTP/1.1 200 OK
{
"ok": true,
}
请求(使用 tag_id 参数筛选指定标签下的博客):
GET /blog?tag_id=1
响应:
HTTP/1.1 200 OK
[
{
"blog_id": 1,
"blog_url": "/blog/1",
"title": "我的第一篇博客",
"tag_id": 1,
"create_time": "2019/05/14 12:00"
},
{
"blog_id": 2,
"blog_url": "/blog/2",
"title": "C 语言的类型",
"tag_id": 1,
"create_time": "2019/05/14 12:00"
}
]
请求:
DELETE /blog/:blog_id
响应:
HTTP/1.1 200 OK
{
"ok": true,
}
剩余29页未读,继续阅读
宝贝的麻麻
- 粉丝: 34
- 资源: 294
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0