![Learn Data Structures and Algorithms with Golang](https://wfqqreader-1252317822.image.myqcloud.com/cover/744/36698744/b_36698744.jpg)
上QQ阅读APP看书,第一时间看更新
The update operation
The update operation is as follows. The UpdateCustomer method takes the Customer parameter and creates a prepared statement for the UPDATE statement. The statement is used to update a customer row in the table:
// Update Customer method with parameter customer
func UpdateCustomer(customer Customer){
var database *sql.DB
database= GetConnection()
var error error
var update *sql.Stmt
update,error = database.Prepare("UPDATE CUSTOMER SET CustomerName=?, SSN=? WHERE CustomerId=?")
if error != nil {
panic(error.Error())
}
update.Exec(customer.CustomerName,customer.SSN,customer.CustomerId)
defer database.Close()
}
// main method
func main() {
var customers []Customer
customers = GetCustomers()
fmt.Println("Before Update",customers)
var customer Customer
customer.CustomerName = "George Thompson"
customer.SSN = "23233432"
customer.CustomerId = 5
UpdateCustomer(customer)
customers = GetCustomers()
fmt.Println("After Update",customers)
}
Run the following commands:
go run database_operations.go
The following screenshot displays the output:
![](https://epubservercos.yuewen.com/AA6936/19470380301498306/epubprivate/OEBPS/Images/8e13fd88-1ac6-41aa-b556-aad717a0f976.png?sign=1739033742-0m6o0h1CzGKODh742ANxkhFe4PqjM4rT-0-1ad4b5c2d2ec174b945dc4418c65609c)
Let's take a look at the delete operation in the next section.