当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.NamedQuery方法代码示例

本文整理汇总了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())
}
开发者ID:gogrademe,项目名称:api,代码行数:15,代码来源:store.go

示例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
}
开发者ID:K4orta,项目名称:lunchclub-api,代码行数:16,代码来源:events.go

示例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
}
开发者ID:K4orta,项目名称:lunchclub-api,代码行数:17,代码来源:locations.go

示例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
}
开发者ID:K4orta,项目名称:tunnel-speed-api,代码行数:19,代码来源:vehicles.go

示例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
}
开发者ID:chop-dbhi,项目名称:sql-agent,代码行数:23,代码来源:connect.go


注:本文中的github.com/jmoiron/sqlx.DB.NamedQuery方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。