當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。