當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Tx.Prepare方法代碼示例

本文整理匯總了Golang中database/sql.Tx.Prepare方法的典型用法代碼示例。如果您正苦於以下問題:Golang Tx.Prepare方法的具體用法?Golang Tx.Prepare怎麽用?Golang Tx.Prepare使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在database/sql.Tx的用法示例。


在下文中一共展示了Tx.Prepare方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: insertTimeSection

func insertTimeSection(tx *sql.Tx, centerId, startTime, endTime string) (id int64, err error) {

	sql := "insert into time_section(center_id,start_time,end_time,lesson_no) values(?,?,?,?)"
	lessgo.Log.Debug(sql)
	stmt, err := tx.Prepare(sql)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	res, err := stmt.Exec(centerId, startTime, endTime, "1")

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	newTimeSectionId, err := res.LastInsertId()
	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	return newTimeSectionId, nil
}
開發者ID:hjqhezgh,項目名稱:wooyou-admin,代碼行數:26,代碼來源:timeSection.go

示例2: insertBinaryAnnotations

func (stor *Storage) insertBinaryAnnotations(tx *sql.Tx, span *models.Span) error {
	stmt, err := tx.Prepare(`INSERT INTO zipkin_annotations VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
	if err != nil {
		return fmt.Errorf("prepare: %+v", err)
	}
	defer stmt.Close()

	for _, ann := range span.BinaryAnnotations {
		// BinaryAnnotation.Value is base64, see: https://github.com/openzipkin/zipkin/blob/master/zipkin-common/src/main/scala/com/twitter/zipkin/json/JsonBinaryAnnotation.scala
		value, err := base64str2sql(ann.Value)
		if err != nil {
			return fmt.Errorf("parse value: %+v", value)
		}

		ipv4, port, service := ep2sql(ann.Endpoint)
		_, err = stmt.Exec(
			hex2int64(span.TraceID),
			hex2int64(span.ID),
			ann.Key,
			value,
			ann.AnnotationType,
			span.Timestamp,
			ipv4, port, service,
		)
		if err != nil {
			return err
		}
	}
	return nil
}
開發者ID:t-yuki,項目名稱:zipkin-query-go,代碼行數:30,代碼來源:store.go

示例3: saveError

//	保存錯誤信息
func saveError(tx *sql.Tx, date, message string) error {

	stmt, err := tx.Prepare("replace into error values(?,?)")
	if err != nil {
		return err
	}
	defer stmt.Close()

	//	新增
	result, err := stmt.Exec(date, message)
	if err != nil {
		return err
	}

	ra, err := result.RowsAffected()
	if err != nil {
		return err
	}

	if ra == 0 {
		return sql.ErrNoRows
	}

	return nil
}
開發者ID:ifzz,項目名稱:stockrecorder,代碼行數:26,代碼來源:sqlite.go

示例4: insertVideoContributorRelation

func insertVideoContributorRelation(tx *sql.Tx, videoId string, contributorId string) {
	// 存在チェック
	stmt, stmtErr := tx.Prepare("SELECT count(video_id) FROM videos_contributors WHERE video_id = ? AND contributor_id = ?")
	if stmtErr != nil {
		panic(stmtErr.Error())
	}
	defer stmt.Close()

	var count int
	err := stmt.QueryRow(videoId, contributorId).Scan(&count)
	if err != nil {
		panic(err.Error())
	}
	defer stmt.Close()

	if count > 0 {
		return
	}

	stmtIns, stmtInsErr := tx.Prepare("INSERT INTO videos_contributors (video_id, contributor_id) VALUES(?, ?)")
	if stmtInsErr != nil {
		panic(stmtInsErr.Error())
	}
	defer stmtIns.Close()

	_, insErr := stmtIns.Exec(videoId, contributorId)
	if insErr != nil {
		panic(insErr.Error())
	}
	defer stmtIns.Close()
}
開發者ID:hogesuke,項目名稱:NicoNewVideoChecker,代碼行數:31,代碼來源:VideoAnalyzer.go

示例5: insertCourse

func insertCourse(tx *sql.Tx, centerId, courseName, courseType string) (id int64, err error) {

	sql := "insert into course(name,center_id,price,is_probation,type,begin_age,end_age,intro,app_display_level,create_time,lesson_num) values(?,?,?,?,?,?,?,?,?,?,?)"
	lessgo.Log.Debug(sql)
	stmt, err := tx.Prepare(sql)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	res, err := stmt.Exec(courseName, centerId, "0", "0", courseType, "0", "60", courseName, "0", time.Now().Format("20060102150405"), "12")

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	newCourseId, err := res.LastInsertId()
	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	return newCourseId, nil
}
開發者ID:hjqhezgh,項目名稱:wooyou-admin,代碼行數:26,代碼來源:course.go

示例6: insertConsumerStatusLog

func insertConsumerStatusLog(tx *sql.Tx, consumerId, employeeId, oldStatus, newStatus string) (id int64, err error) {

	sql := "insert into consumer_status_log(consumer_id,employee_id,create_time,old_status,new_status) values(?,?,?,?,?)"
	lessgo.Log.Debug(sql)
	stmt, err := tx.Prepare(sql)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	res, err := stmt.Exec(consumerId, employeeId, time.Now().Format("20060102150405"), oldStatus, newStatus)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	logId, err := res.LastInsertId()

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	return logId, err
}
開發者ID:hjqhezgh,項目名稱:wooyou-admin,代碼行數:27,代碼來源:consumerStatusLog.go

示例7: StoreOIP041Artifact

func StoreOIP041Artifact(o Oip041, txid string, block int, dbtx *sql.Tx) error {
	// store in database
	stmtStr := `INSERT INTO 'oip_artifact'
		('active','block','json','tags','timestamp',
		'title','txid','type','year','publisher')
		VALUES (?,?,?,?,?,?,?,?,?,?);`

	stmt, err := dbtx.Prepare(stmtStr)
	if err != nil {
		return err
	}
	defer stmt.Close()

	s, err := o.GetJSON()
	if err != nil {
		return nil
	}

	_, err = stmt.Exec(1, block, s, strings.Join(o.Artifact.Info.ExtraInfo.Tags, ","),
		o.Artifact.Timestamp, o.Artifact.Info.Title, txid, o.Artifact.Type,
		o.Artifact.Info.Year, o.Artifact.Publisher)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:dloa,項目名稱:media-protocol,代碼行數:27,代碼來源:oip041-artifact.go

示例8: APIGetAllOIP041

func APIGetAllOIP041(dbtx *sql.Tx) ([]Oip041ArtifactAPIResult, error) {
	stmtStr := `select a.block, a.json, a.tags, a.timestamp,
				a.title, a.txid, a.type, a.year, a.publisher
				from oip_artifact as a join publisher as p
				where p.address = a.publisher and a.invalidated = 0`

	stmt, err := dbtx.Prepare(stmtStr)
	if err != nil {
		return []Oip041ArtifactAPIResult{}, err
	}
	defer stmt.Close()

	rows, err := stmt.Query()
	if err != nil {
		return []Oip041ArtifactAPIResult{}, err
	}

	var results []Oip041ArtifactAPIResult

	for rows.Next() {
		var a Oip041ArtifactAPIResult
		var s string

		rows.Scan(&a.Block, &s, &a.Tags, &a.Timestamp,
			&a.Title, &a.TxID, &a.Type, &a.Year, &a.Publisher)

		json.Unmarshal([]byte(s), &a.OIP041)
		results = append(results, a)
	}

	stmt.Close()
	rows.Close()

	return results, nil
}
開發者ID:dloa,項目名稱:media-protocol,代碼行數:35,代碼來源:oip041.go

示例9: Delete

// Delete will delete all of the configurations whose name is in the list
// of names in the arugment. It will not return an error if the name is not found.
func (cc *ConfigurationController) Delete(names ...string) (err error) {
	var (
		tx   *sql.Tx
		stmt *sql.Stmt
	)
	tx, err = cc.DB.Begin()
	if err != nil {
		tx.Rollback()
		return err
	}

	stmt, err = tx.Prepare("DELETE FROM configurations where config_name = $1")
	if err != nil {
		tx.Rollback()
		return err
	}

	for _, name := range names {
		_, err := stmt.Exec(name)

		if err == sql.ErrNoRows {
			err = nil
		}
		if err != nil {
			tx.Rollback()
			return err
		}
	}

	return tx.Commit()

}
開發者ID:wrrn,項目名稱:restapi,代碼行數:34,代碼來源:configuration.go

示例10: Up_20160429102901

// Up is executed when this migration is applied
func Up_20160429102901(txn *sql.Tx) {
	var sql string = "create table if not exists users ( " +
		"id int(10) unsigned not null auto_increment, " +
		"created_at int(13) unsigned not null, " +
		"updated_at int(13) unsigned not null, " +
		"name varchar(100) not null unique, " +
		"screen_name varchar(100), " +
		"mail varchar(100) not null unique, " +
		"phone varchar(100) unique, " +
		"avatar varchar(200), " +
		"gender varchar(10), " +
		"birth int(13), " +
		"description text, " +
		"type varchar(16), " +
		"password varchar(100), " +
		"last_login_at int(13), " +
		"PRIMARY KEY(id) " + ")"
	stmt, err := txn.Prepare(sql)
	if err != nil {
		panic(err.Error())
	}
	_, err = stmt.Exec()

	if err != nil {
		panic(err.Error())
	}
}
開發者ID:ts25504,項目名稱:MuShare,代碼行數:28,代碼來源:20160429102901_CreateUser.go

示例11: insertParent

func insertParent(tx *sql.Tx, name, password, telephone, comeForm string) (id int64, err error) {

	sql := "insert into parent(name,password,telephone,reg_date,come_form) values(?,?,?,?,?)"
	lessgo.Log.Debug(sql)
	stmt, err := tx.Prepare(sql)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	res, err := stmt.Exec(name, password, telephone, time.Now().Format("20060102150405"), comeForm)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	newParentId, err := res.LastInsertId()
	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	return newParentId, nil
}
開發者ID:hjqhezgh,項目名稱:wooyou-admin,代碼行數:26,代碼來源:parent.go

示例12: insertConsumerContactsLog

func insertConsumerContactsLog(tx *sql.Tx, createUser, note, consumerId, contactsType string) (id int64, err error) {

	sql := "insert into consumer_contact_log(create_user,create_time,note,consumer_id,type) values(?,?,?,?,?) "
	lessgo.Log.Debug(sql)
	stmt, err := tx.Prepare(sql)
	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	res, err := stmt.Exec(createUser, time.Now().Format("20060102150405"), note, consumerId, contactsType)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	consumerContactsLogId, err := res.LastInsertId()

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	return consumerContactsLogId, nil
}
開發者ID:hjqhezgh,項目名稱:wooyou-admin,代碼行數:26,代碼來源:consumerContactLog.go

示例13: insertConsumer

func insertConsumer(tx *sql.Tx, centerId, contactStatus, homePhone, child, year, month, birthday, lastContactTime, comeFromId, createUser, parttimeName string) (id int64, err error) {

	sql := "insert into consumer_new(center_id,contact_status,home_phone,create_time,child,year,month,birthday,last_contact_time,come_from_id,create_user_id,parttime_name) values(?,?,?,?,?,?,?,?,?,?,?,?)"
	lessgo.Log.Debug(sql)
	stmt, err := tx.Prepare(sql)
	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	res, err := stmt.Exec(centerId, contactStatus, homePhone, time.Now().Format("20060102150405"), child, year, month, birthday, lastContactTime, comeFromId, createUser, parttimeName)

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	consumerId, err := res.LastInsertId()

	if err != nil {
		lessgo.Log.Error(err.Error())
		return 0, err
	}

	return consumerId, err
}
開發者ID:hjqhezgh,項目名稱:wooyou-admin,代碼行數:26,代碼來源:consumer.go

示例14: SaveAttrs

func SaveAttrs(log *logging.Logger, tx *sql.Tx, section *common.Section, metadata common.JSONFile) error {
	var stmt *sql.Stmt
	var err error

	var insert = "INSERT INTO attrs" +
		"(area, grace, dataset, element, os, username, groupname, type," +
		" link, mtime, ctime, hash, perms, compressed)" +
		" VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)"

	stmt, err = tx.Prepare(insert)
	if err != nil {
		log.Debug("Failed prepare: " + err.Error())
		return errorMsg(log, 3, "Cannot save Attributes for element "+section.Name)
	}

	_, err = stmt.Exec(section.Name, section.Grace, section.Dataset, metadata.Name,
		metadata.Os, metadata.User, metadata.Group, metadata.Type, metadata.Link,
		metadata.Mtime, metadata.Ctime, metadata.Hash, metadata.Mode, section.Compressed)

	if err != nil {
		log.Debug("Failed execute: " + err.Error())
		return errorMsg(log, 4, "Cannot save Attributes for element "+section.Name)
	}

	stmt.Close()
	return nil
}
開發者ID:mementobackup,項目名稱:server,代碼行數:27,代碼來源:queries.go

示例15: consumeMsgIDsToDismiss

func (s *SQLEngine) consumeMsgIDsToDismiss(tx *sql.Tx, u gregor.UID, mid gregor.MsgID, dmids []gregor.MsgID, ctime time.Time) error {

	// early out if no msg IDs to dismiss were specified
	if len(dmids) == 0 {
		return nil
	}

	ins, err := tx.Prepare("INSERT INTO gregor_dismissals_by_id(uid, msgid, dmsgid) VALUES(?, ?, ?)")
	if err != nil {
		return err
	}
	defer ins.Close()
	upd, err := tx.Prepare("UPDATE gregor_items SET dtime=? WHERE uid=? AND msgid=?")
	if err != nil {
		return err
	}
	defer upd.Close()

	ctimeArg := s.newQueryBuilder().TimeArg(ctime)
	hexUID := hexEnc(u)
	hexMID := hexEnc(mid)

	for _, dmid := range dmids {
		_, err = ins.Exec(hexUID, hexMID, hexEnc(dmid))
		if err != nil {
			return err
		}
		_, err = upd.Exec(ctimeArg, hexUID, hexEnc(dmid))
		if err != nil {
			return err
		}
	}
	return err
}
開發者ID:qbit,項目名稱:client,代碼行數:34,代碼來源:sql_sm.go


注:本文中的database/sql.Tx.Prepare方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。