Middle+
Which team is used to insert a value into a table?
sobes.tech AI
Answer from AI
To insert a value into a table in SQL, the INSERT INTO command is used. In the context of Go (Golang) when working with a database through the database/sql package, an SQL query with this command is usually constructed and executed using the Exec or Prepare methods.
Example of inserting a record into the users table with name and age fields:
query := "INSERT INTO users (name, age) VALUES (?, ?)"
result, err := db.Exec(query, "Ivan", 30)
if err != nil {
log.Fatal(err)
}
id, err := result.LastInsertId()
if err != nil {
log.Fatal(err)
}
fmt.Println("Record inserted with ID:", id)
Here, ? are placeholders for parameters, which are safely substituted into the query.