本文整理汇总了Golang中github.com/jmoiron/sqlx.DB.NamedQuery方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.NamedQuery方法的具体用法?Golang DB.NamedQuery怎么用?Golang DB.NamedQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jmoiron/sqlx.DB
的用法示例。
在下文中一共展示了DB.NamedQuery方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: insert
func insert(db *sqlx.DB, stmt string, params interface{}) (int, error) {
rows, err := db.NamedQuery(stmt, params)
if err != nil {
return 0, err
}
defer rows.Close()
if rows.Next() {
var id int
err := rows.Scan(&id)
return id, err
}
return 0, errors.New("No serial value returned for insert: " + stmt + ", error: " + rows.Err().Error())
}
示例2: InsertEvent
// InsertEvent takes a DB connect and inserts a new EventEntry
func InsertEvent(db *sqlx.DB, event *models.Event) (*models.Event, error) {
rows, err := db.NamedQuery(db.Rebind(`
INSERT INTO events (title, slug, start_time, end_time, organizer_id, location_id, rsvps)
VALUES (:title, :slug, :start_time, :end_time, :organizer_id, :location_id, :rsvps)
RETURNING id`), event)
if err != nil {
return nil, err
}
if rows.Next() {
rows.Scan(&event.ID)
}
return event, nil
}
示例3: InsertLocation
// InsertLocation inserts a new Location object into the DB
func InsertLocation(db *sqlx.DB, location *models.Location) (*models.Location, error) {
rows, err := db.NamedQuery(db.Rebind(`
INSERT INTO locations (name, slug, address, lat_lng)
VALUES (:name, :slug, :address, :lat_lng)
RETURNING id
`), location)
if err != nil {
return nil, err
}
if rows.Next() {
rows.Scan(&location.ID)
}
return location, nil
}
示例4: InsertVehicle
// InsertVehicle adds a vehicle to the database
func InsertVehicle(db *sqlx.DB, v *muni.Vehicle) error {
rows, err := db.NamedQuery(`
INSERT INTO vehicles (
route_tag, vehicle_id, time_received, heading,
dir_tag, lat, lng, leading_vehicle_id, predictable,
secs_since_report, speed_km_hr
)
VALUES (
:route_tag, :vehicle_id, :time_received, :heading,
:dir_tag, :lat, :lng, :leading_vehicle_id, :predictable,
:secs_since_report, :speed_km_hr
);`, v)
if err != nil {
return err
}
rows.Close()
return nil
}
示例5: Execute
// Execute takes a database instance, SQL statement, and parameters and executes the query
// returning the resulting rows.
func Execute(db *sqlx.DB, sql string, params map[string]interface{}) (*Iterator, error) {
var (
err error
rows *sqlx.Rows
)
// Execute the query.
if params != nil && len(params) > 0 {
rows, err = db.NamedQuery(sql, params)
} else {
rows, err = db.Queryx(sql)
}
if err != nil {
return nil, err
}
return &Iterator{
rows: rows,
}, nil
}