mirror of https://github.com/go-gorm/gorm.git
46 lines
993 B
Markdown
46 lines
993 B
Markdown
## Raw SQL
|
|
|
|
```go
|
|
db.Exec("DROP TABLE users;")
|
|
db.Exec("UPDATE orders SET shipped_at=? WHERE id IN (?)", time.Now, []int64{11,22,33})
|
|
```
|
|
|
|
## Row & Rows
|
|
|
|
It is even possible to get query result as `*sql.Row` or `*sql.Rows`
|
|
|
|
```go
|
|
row := db.Table("users").Where("name = ?", "jinzhu").Select("name, age").Row() // (*sql.Row)
|
|
row.Scan(&name, &age)
|
|
|
|
rows, err := db.Model(&User{}).Where("name = ?", "jinzhu").Select("name, age, email").Rows() // (*sql.Rows, error)
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
...
|
|
rows.Scan(&name, &age, &email)
|
|
...
|
|
}
|
|
|
|
// Raw SQL
|
|
rows, err := db.Raw("select name, age, email from users where name = ?", "jinzhu").Rows() // (*sql.Rows, error)
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
...
|
|
rows.Scan(&name, &age, &email)
|
|
...
|
|
}
|
|
```
|
|
|
|
### Scan Rows
|
|
|
|
```go
|
|
rows, err := db.Model(&User{}).Where("name = ?", "jinzhu").Select("name, age, email").Rows() // (*sql.Rows, error)
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var user User
|
|
db.ScanRows(rows, &user)
|
|
// do something
|
|
}
|
|
```
|