本文整理匯總了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
}