How to use C++ to implement word Management system
This article mainly shows you "how to use C++ to achieve a word management system". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn this article "how to use C++ to implement a word management system".
The details are as follows
Realize the function
Quit
Add words
Delete a word
Modify words
Query words
Sort words
Display words
Brief introduction
The word management system uses C++ language to connect to the MySQL database to achieve routine CRUD operations, and the interface is the console form + text display.
Test environment
Written in Win10 + Code::Blocks IDE.
Run screenshot
Code # include # include / / Network connection # include / / MySQL C API access mysql database # pragma comment (lib, "libmysql.lib") # define N 50 typedef struct Dictionary {char id [50]; char eng [100]; char chi [100];} Dictionary; / / variable configuration MYSQL * conn; / / database connection handle MYSQL_RES * res; / / execute database language results MYSQL_ROW row / / storing a data record char* server = "localhost"; / / Local connection char* user = "root"; / / char* password = "yan19991001"; / / mysql password char* database = "dictionary"; / / Database name int tmae rcterchar query [N]; / / statements to be queried 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 ("query statement:% s\ n", query); rc = mysql_query (conn,query); if (0! = rc) {printf ("mysql_real_query ():% s\ n", mysql_error (conn)); return-1 } else {printf ("query result:\ n"); res = mysql_use_result (conn); / / get the result if (res) {while ((row = mysql_fetch_row (res))! = NULL) {/ / printf ("num=%d\ n", mysql_num_fields (res)) / / number of columns for (t = 0; t < mysql_num_fields (res); tweak +) printf ("% 8s", row [t]); printf ("\ n");}} mysql_free_result (res);}} void addEng () {char id [N], eng [N], chi [N] Printf ("Please enter dictionary information to be added:\ n"); printf ("serial number:\ n"); scanf ("% s", id); printf ("word:\ n"); scanf ("% s", eng); printf ("Chinese definition:\ n"); scanf ("% s", chi) Sprintf (query, "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 (" added successfully!\ n ") } / / mysql_close (conn); / / disconnect the database} void delEng () {char id [N]; printf ("Please enter the word number you want to delete:"); scanf ("% s", id); sprintf (query, "delete from test where id ='% s'", id); printf ("query statement:% s\ n", query); rc = mysql_query (conn,query) If (0! = rc) {printf ("mysql_real_query ():% s\ n", mysql_error (conn)); return-1;} else {printf ("deleted successfully!\ n");}} void modEng () {char id [N], eng [N], chi [N]; printf ("Please enter the word number you want to modify:"); scanf ("% s", id) Printf ("word:\ n"); scanf ("% s", eng); printf ("Chinese definition:\ n"); scanf ("% s", chi); sprintf (query, "update test set eng ='% s' where id ='% s'", eng,chi,id); printf ("query statement:% s\ n", query); rc = mysql_query (conn,query) If (0! = rc) {printf ("mysql_real_query ():% s\ n", mysql_error (conn)); return-1;} else {printf ("modified successfully!\ n");}} void seaEng () {char id [N], eng [N], chi [N]; printf ("Please enter the word number you want to query:"); scanf ("% s", id) Sprintf (query, "select * from test where id ='% s'", id); printf ("query statement:% s\ n", query); rc = mysql_query (conn,query); if (0! = rc) {printf ("mysql_real_query ():% s\ n", mysql_error (conn)); return-1;} else {printf ("query results:\ n") Res = mysql_use_result (conn); / / get the result if (res) {while ((row = mysql_fetch_row (res))! = NULL) {/ / printf ("num=%d\ n", mysql_num_fields (res)); / / number of columns for (t = 0; t < mysql_num_fields (res)) Tweak +) printf ("% 8s", row [t]); printf ("\ n");}} mysql_free_result (res);}} void init () {conn = mysql_init (NULL) / / handle initialization if (! mysql_real_connect (conn, server, user, password, database, 3306, NULL, 0)) / / determine whether the connection is successful {printf ("Error connecting to database:%s\ n", mysql_error (conn));} else {printf ("Connected...\ n") } / / character encoding to solve garbled 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" / / statement if (mysql_query (conn, query)) {printf ("error message:% s\ n", mysql_error (conn));} else {printf ("query result:\ n"); res = mysql_use_result (conn) / / get the result if (res) {while ((row = mysql_fetch_row (res))! = NULL) {/ / printf ("num=%d\ n", mysql_num_fields (res)); / / number of columns for (t = 0; t < mysql_num_fields (res)) Tweak +) printf ("% 8s", row [t]); printf ("\ n");}} mysql_free_result (res);}} void menu () {int choice; char id [20] Do {printf ("- -\ n"); printf ("0, exit\ n"); printf ("1, add words\ n"); printf ("2, delete words\ n"); printf ("3, modify words\ n") Printf ("4, query words\ n"); printf ("5, sort words\ n"); printf ("6, display words\ n"); printf ("- -\ n"); printf ("Please enter selection:"); scanf ("% d", & choice) / / Select function 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 ("input error!");} system ("pause"); system ("cls") } while (choice! = 0);} int main () {init (); menu (); return 0;}
Database code
/ * 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: 28Unip 06Compact 2021 1318 44 Swiss 35 pounds / 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 ('1th,' adopt', 'adoption'); INSERT INTO `test`VALUES ('2th,' pen', 'pen'); INSERT INTO `test`VALUES ('34th,' apple', 'apple') INSERT INTO `test` VALUES ('4percent,' borrow', 'loan'); INSERT INTO `test` VALUES ('5percent,' electric', 'electricity'); SET FOREIGN_KEY_CHECKS = 1. These are all the contents of the article "how to use C++ to implement a word management system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!