用go 语言用mysql写一个简易的客户管理系统
时间: 2023-07-18 16:31:35 浏览: 43
好的,以下是一个使用 MySQL 数据库实现的客户管理系统的 Go 语言代码示例:
首先需要安装 MySQL 驱动,可以使用以下命令来安装:
```
go get -u github.com/go-sql-driver/mysql
```
然后创建一个名为 `customers` 的数据库,以及一个名为 `customer` 的表,表结构如下:
```
CREATE TABLE `customer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
接下来是 Go 代码:
```go
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type Customer struct {
id int
name string
email string
phone string
}
var db *sql.DB
func main() {
// 连接数据库
var err error
db, err = sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/customers")
if err != nil {
fmt.Println(err)
return
}
defer db.Close()
for {
fmt.Println("Welcome to the Customer Management System!")
fmt.Println("----------------------------------------")
fmt.Println("1. Add Customer")
fmt.Println("2. View Customers")
fmt.Println("3. Exit")
fmt.Println("----------------------------------------")
var choice int
fmt.Print("Enter your choice: ")
fmt.Scanln(&choice)
switch choice {
case 1:
addCustomer()
case 2:
viewCustomers()
case 3:
fmt.Println("Goodbye!")
return
default:
fmt.Println("Invalid choice. Please try again.")
}
}
}
func addCustomer() {
var name, email, phone string
fmt.Print("Enter customer name: ")
fmt.Scanln(&name)
fmt.Print("Enter customer email: ")
fmt.Scanln(&email)
fmt.Print("Enter customer phone: ")
fmt.Scanln(&phone)
statement, err := db.Prepare("INSERT INTO customer (name, email, phone) VALUES (?, ?, ?)")
if err != nil {
fmt.Println(err)
return
}
defer statement.Close()
result, err := statement.Exec(name, email, phone)
if err != nil {
fmt.Println(err)
return
}
rowsAffected, err := result.RowsAffected()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Customer added successfully! %d rows affected.\n", rowsAffected)
}
func viewCustomers() {
rows, err := db.Query("SELECT * FROM customer")
if err != nil {
fmt.Println(err)
return
}
defer rows.Close()
fmt.Println("Customer List:")
fmt.Println("------------------------")
for rows.Next() {
var customer Customer
err := rows.Scan(&customer.id, &customer.name, &customer.email, &customer.phone)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("ID: %d\nName: %s\nEmail: %s\nPhone: %s\n\n", customer.id, customer.name, customer.email, customer.phone)
}
}
```
这个程序使用 `database/sql` 包来连接 MySQL 数据库,然后通过 `sql.DB` 来执行 SQL 语句。`addCustomer()` 函数用来添加新的客户,先使用 `db.Prepare()` 方法来准备 SQL 语句,然后使用 `statement.Exec()` 方法来执行插入操作。`viewCustomers()` 函数用来查看所有客户信息,使用 `db.Query()` 方法来执行查询操作,然后使用 `rows.Scan()` 方法将查询结果扫描到 `Customer` 结构体中。注意,需要在程序结束时关闭数据库连接,可以使用 `defer` 关键字来实现。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)