Skip to main content

Using Raw SQL

While the methods provided by Model can handle most data operations, complex scenarios may require custom SQL queries.

Query

db := g.DB()
books, err := db.Query(ctx, "SELECT * FROM `book` WHERE `id` > ? AND `id` < ?", g.Array{3, 7})

Insert Data

db := g.DB()
sql := "INSERT INTO `book` (`name`, `author`, `price`) VALUES (?, ?, ?)"
data := g.Array{"Go Programming from Beginner to Mastery", "Go Study Group", 99.98}
result, err := db.Exec(ctx, sql, data)

For more operations, refer to the official documentation ORM Methods (Raw)