C++实现单词管理系统
时间:2022-12-22 09:54:54|栏目:C代码|点击: 次
本文实例为大家分享了C++实现单词管理系统的具体代码,供大家参考,具体内容如下
实现功能
- 退出
- 添加单词
- 删除单词
- 修改单词
- 查询单词
- 排序单词
- 显示单词
简述
单词管理系统使用了C++语言连接MySQL数据库实现常规CRUD操作,界面为控制台窗体+文字显示的方式。
测试环境
使用Win10 + Code::Blocks IDE编写。
运行截图
代码
#include <stdio.h> #include <winsock2.h> //进行网络连接 #include <mysql.h> //MySQL C API访问mysql数据库 #pragma comment (lib, "libmysql.lib") #define N 50 typedef struct Dictionary { char id[50]; char eng[100]; char chi[100]; } Dictionary; //变量配置 MYSQL *conn; //数据库连接句柄 MYSQL_RES *res; //执行数据库语言结果 MYSQL_ROW row; //存放一个数据记录 char* server = "localhost";//本地连接 char* user = "root";// char* password = "yan19991001";//mysql密码 char* database = "dictionary";//数据库名 int t,rc; char query[N]; //需要查询的语句 void readEng(); void addEng(); void delEng(); void modEng(); void seaEng(); void sort(); void sort(){ char id[N],eng[N],chi[N]; sprintf(query,"select * from test order by eng"); printf("查询语句:%s\n",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("查询结果:\n"); res = mysql_use_result(conn); //获取结果 if (res) { while ((row = mysql_fetch_row(res)) != NULL) { //printf("num=%d\n",mysql_num_fields(res));//列数 for (t = 0; t < mysql_num_fields(res); t++) printf("%8s ", row[t]); printf("\n"); } } mysql_free_result(res); } } void addEng() { char id[N],eng[N],chi[N]; printf("请输入要增加的词典信息:\n"); printf("编号:\n"); scanf("%s",id); printf("单词:\n"); scanf("%s",eng); printf("中文释义:\n"); scanf("%s",chi); sprintf(query,"insert into test values('%s','%s','%s')",id,eng,chi); printf("%s",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("添加成功!\n"); } //mysql_close(conn); //断开数据库 } void delEng(){ char id[N]; printf("请输入你要删除的单词编号:"); scanf("%s",id); sprintf(query,"delete from test where id = '%s'",id); printf("查询语句:%s\n",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("删除成功!\n"); } } void modEng(){ char id[N],eng[N],chi[N]; printf("请输入你要修改的单词编号:"); scanf("%s",id); printf("单词:\n"); scanf("%s",eng); printf("中文释义:\n"); scanf("%s",chi); sprintf(query,"update test set eng = '%s',chi = '%s' where id = '%s'",eng,chi,id); printf("查询语句:%s\n",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("修改成功!\n"); } } void seaEng(){ char id[N],eng[N],chi[N]; printf("请输入你要查询的单词编号:"); scanf("%s",id); sprintf(query,"select * from test where id = '%s'",id); printf("查询语句:%s\n",query); rc = mysql_query(conn,query); if (0 != rc) { printf("mysql_real_query(): %s\n", mysql_error(conn)); return -1; }else{ printf("查询结果:\n"); res = mysql_use_result(conn); //获取结果 if (res) { while ((row = mysql_fetch_row(res)) != NULL) { //printf("num=%d\n",mysql_num_fields(res));//列数 for (t = 0; t < mysql_num_fields(res); t++) printf("%8s ", row[t]); printf("\n"); } } mysql_free_result(res); } } void init() { conn = mysql_init(NULL); //句柄初始化 if (!mysql_real_connect(conn, server, user, password, database, 3306, NULL, 0)) //判断是否连接成功 { printf("Error connecting to database:%s\n", mysql_error(conn)); } else { printf("Connected...\n"); } //字符编码,解决乱码 if (!mysql_set_character_set(conn, "gbk")) { printf("New client character set: %s\n", mysql_character_set_name(conn)); } } void readEng() { char * query = "select * from test"; //需要查询的语句 if (mysql_query(conn, query)) { printf("错误信息:%s\n", mysql_error(conn)); } else { printf("查询结果:\n"); res = mysql_use_result(conn); //获取结果 if (res) { while ((row = mysql_fetch_row(res)) != NULL) { //printf("num=%d\n",mysql_num_fields(res));//列数 for (t = 0; t < mysql_num_fields(res); t++) printf("%8s ", row[t]); printf("\n"); } } mysql_free_result(res); } } void menu() { int choice; char id[20]; do { printf("------------------------------\n"); printf("0、退出\n"); printf("1、添加单词\n"); printf("2、删除单词\n"); printf("3、修改单词\n"); printf("4、查询单词\n"); printf("5、排序单词\n"); printf("6、显示单词\n"); printf("------------------------------\n"); printf("请输入选择:"); scanf("%d",&choice); //根据choice的值选取功能 switch(choice) { case 0: exit(0); break; case 1: addEng(); break; case 2: delEng(); break; case 3: modEng(); break; case 4: seaEng(); break; case 5: sort(); break; case 6: readEng(); break; default: printf("输入错误!"); } system("pause"); system("cls"); } while(choice != 0); } int main() { init(); menu(); return 0; }
数据库代码
/* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Type : MySQL Source Server Version : 50725 Source Host : localhost:3306 Source Schema : dictionary Target Server Type : MySQL Target Server Version : 50725 File Encoding : 65001 Date: 28/06/2021 13:44:35 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for test -- ---------------------------- DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `eng` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `chi` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of test -- ---------------------------- INSERT INTO `test` VALUES ('1', 'adopt', '领养'); INSERT INTO `test` VALUES ('2', 'pen', '钢笔'); INSERT INTO `test` VALUES ('3', 'apple', '苹果'); INSERT INTO `test` VALUES ('4', 'borrow', '借阅'); INSERT INTO `test` VALUES ('5', 'electric', '电力'); SET FOREIGN_KEY_CHECKS = 1;
总结
代码还是比较简单的,主要是不同编译器,它所对应的驱动方式会有所不同。因此如果想要移植到其它的IDE如: VC6++、VS、DEV 等,可能需要一些处理操作,还要添加数据库连接驱动和库函数。当然,难点也就在于获取ODBC连接,这块还是需要一些时间琢磨的。