Home > Database > Mysql Tutorial > How to develop a simple memo function using MySQL and C++

How to develop a simple memo function using MySQL and C++

WBOY
Release: 2023-09-22 09:33:37
Original
916 people have browsed it

How to develop a simple memo function using MySQL and C++

How to use MySQL and C to develop a simple memo function

Memo is a tool commonly used in our daily life. It can help us record important matters and remind us. Tasks etc. In this article, we will develop a simple memo functionality using MySQL and C programming language.

First, we need to prepare the development environment. Make sure you have installed the MySQL database and a C development environment, such as Visual Studio or Code::Blocks, etc. Next, we will gradually implement each functional module of the memo.

  1. Create database table

First, we need to create a database table to store the memo entries. In MySQL, we can use the following SQL statement to create a table named memos:

CREATE TABLE memos (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Copy after login

The above SQL statement creates a memos table, including id (auto-incrementing primary key), title (memo title), content (memo content) and created_at (creation time) fields.

  1. C Code

Next, we will use C to write code to connect to the MySQL database and implement various functions of the memo.

First, we need to include the necessary header files, such as mysql.h and string.h. Then, we can use the following code to connect to the MySQL database:

#include <mysql.h>
#include <string.h>

int main() {
    MYSQL* conn = mysql_init(NULL);
    
    if (mysql_real_connect(conn, "localhost", "root", "password", "database", 0, NULL, 0)) {
        printf("Connected to MySQL database.
");
    } else {
        printf("Failed to connect to MySQL database.
");
        return 0;
    }
    
    mysql_close(conn);
    return 0;
}
Copy after login

In the above code, we use the mysql_init function to initialize a MYSQL object, and then use the mysql_real_connect function to connect to the MySQL database. You need to replace "localhost" with the hostname of the database, "root" with the username of the database, "password" with the password of the database, and "database" with the name of the database.

  1. Add memo entries

Next, we will implement the function of inserting memo entries into the database. We can use the following code to achieve:

#include <mysql.h>
#include <string.h>

int main() {
    MYSQL* conn = mysql_init(NULL);
    
    if (mysql_real_connect(conn, "localhost", "root", "password", "database", 0, NULL, 0)) {
        printf("Connected to MySQL database.
");
        
        // 输入备忘录标题和内容
        char title[255];
        char content[1000];
        printf("Enter memo title: ");
        gets(title);
        printf("Enter memo content: ");
        gets(content);
        
        // 构建插入SQL语句
        char sql[500];
        sprintf(sql, "INSERT INTO memos (title, content) VALUES ('%s', '%s')", title, content);
        
        // 执行插入SQL语句
        if (mysql_query(conn, sql) == 0) {
            printf("Memo added successfully.
");
        } else {
            printf("Failed to add memo.
");
        }
    } else {
        printf("Failed to connect to MySQL database.
");
        return 0;
    }
    
    mysql_close(conn);
    return 0;
}
Copy after login

In the above code, we first enter the title and content of the memo, and use the sprintf function to construct the insert SQL statement. Then, use the mysql_query function to execute the insertion SQL statement. If the insertion is successful, a success message will be prompted.

  1. Display memo entries

Next, we will implement the function of obtaining memo entries from the database and displaying them. We can use the following code to achieve this:

#include <mysql.h>
#include <string.h>

int main() {
    MYSQL* conn = mysql_init(NULL);
    
    if (mysql_real_connect(conn, "localhost", "root", "password", "database", 0, NULL, 0)) {
        printf("Connected to MySQL database.
");
        
        MYSQL_RES* res;
        MYSQL_ROW row;
        
        // 执行查询SQL语句
        if (mysql_query(conn, "SELECT * FROM memos")) {
            printf("Failed to execute query.
");
            return 0;
        }
        
        // 获取查询结果集
        res = mysql_use_result(conn);
        
        // 循环遍历结果集并输出备忘录条目
        while ((row = mysql_fetch_row(res)) != NULL) {
            printf("Memo ID: %s
", row[0]);
            printf("Title: %s
", row[1]);
            printf("Content: %s
", row[2]);
            printf("Created at: %s
", row[3]);
            printf("---------------------
");
        }
        
        // 释放结果集
        mysql_free_result(res);
    } else {
        printf("Failed to connect to MySQL database.
");
        return 0;
    }
    
    mysql_close(conn);
    return 0;
}
Copy after login

In the above code, we use the mysql_query function to execute the query SQL statement, and use mysql_use_result to obtain the query result set. Then, use the mysql_fetch_row function to loop through the result set and output the memo entries.

  1. Delete memo entries

Finally, we will implement the function of deleting memo entries. We can use the following code to achieve this:

#include <mysql.h>
#include <string.h>

int main() {
    MYSQL* conn = mysql_init(NULL);
    
    if (mysql_real_connect(conn, "localhost", "root", "password", "database", 0, NULL, 0)) {
        printf("Connected to MySQL database.
");
        
        // 输入要删除的备忘录ID
        int memoId;
        printf("Enter memo ID to delete: ");
        scanf("%d", &memoId);
        
        // 构建删除SQL语句
        char sql[200];
        sprintf(sql, "DELETE FROM memos WHERE id = %d", memoId);
        
        // 执行删除SQL语句
        if (mysql_query(conn, sql) == 0) {
            printf("Memo deleted successfully.
");
        } else {
            printf("Failed to delete memo.
");
        }
    } else {
        printf("Failed to connect to MySQL database.
");
        return 0;
    }
    
    mysql_close(conn);
    return 0;
}
Copy after login

In the above code, we first enter the ID of the memo to be deleted, and use the sprintf function to construct the delete SQL statement. Then, use the mysql_query function to execute the deletion SQL statement. If the deletion is successful, a success message will be prompted.

Through the above steps, we have completed the development of a simple memo function. You can expand and optimize functions according to actual needs. I hope this article will be helpful to develop the memo function using MySQL and C!

The above is the detailed content of How to develop a simple memo function using MySQL and C++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template