How to use MySQL and Go language to develop a simple schedule reminder system
With the fast pace of modern life and busy work, we often ignore important things and dating. In order to help people better manage time, we can use MySQL and Go language to develop a simple schedule reminder system. This article will introduce how to store schedule information through a MySQL database and write related code using Go language.
First, we need to design a database to store schedule information. We can create a table named "events", containing the following fields:
can be used The following SQL statement creates this table:
CREATE TABLE events ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, date DATE NOT NULL, time TIME NOT NULL, description TEXT );
Next, we use Go language to write code to connect to the MySQL database and implement related functions. First, we need to import the necessary packages:
package main import ( "database/sql" "fmt" "log" _ "github.com/go-sql-driver/mysql" )
Then, we define an event structure to store the event information retrieved from the database:
type Event struct { ID int Title string Date string Time string Description string }
Next, we write a function to retrieve the event information from the database Get all events in the database:
func getEvents(db *sql.DB) ([]Event, error) { rows, err := db.Query("SELECT * FROM events") if err != nil { return nil, err } defer rows.Close() var events []Event for rows.Next() { var event Event err := rows.Scan(&event.ID, &event.Title, &event.Date, &event.Time, &event.Description) if err != nil { return nil, err } events = append(events, event) } if err = rows.Err(); err != nil { return nil, err } return events, nil }
Next, we write a function to create new events:
func createEvent(db *sql.DB, event Event) (int64, error) { stmt, err := db.Prepare("INSERT INTO events (title, date, time, description) VALUES (?, ?, ?, ?)") if err != nil { return -1, err } defer stmt.Close() result, err := stmt.Exec(event.Title, event.Date, event.Time, event.Description) if err != nil { return -1, err } id, err := result.LastInsertId() if err != nil { return -1, err } return id, nil }
Finally, we write a main function to test these functions:
func main() { db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/database") if err != nil { log.Fatal(err) } defer db.Close() events, err := getEvents(db) if err != nil { log.Fatal(err) } for _, event := range events { fmt.Println(event.Title, event.Date, event.Time, event.Description) } event := Event{ Title: "Meeting", Date: "2021-01-01", Time: "10:00:00", Description: "Important meeting with clients", } id, err := createEvent(db, event) if err != nil { log.Fatal(err) } fmt.Println("Created event with ID:", id) }
The above is a code example for developing a simple schedule reminder system using MySQL and Go language. Through this system, we can easily store and retrieve schedule information and create new events. You can further develop and optimize according to actual needs. Hope this article is helpful to you!
The above is the detailed content of How to develop a simple schedule reminder system using MySQL and Go language. For more information, please follow other related articles on the PHP Chinese website!