本文整理汇总了Golang中database/sql.Stmt.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang Stmt.Close方法的具体用法?Golang Stmt.Close怎么用?Golang Stmt.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类database/sql.Stmt
的用法示例。
在下文中一共展示了Stmt.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Persist
func (fav Favorite) Persist() error {
var stmtIns *sql.Stmt
var err error
if fav.id == 0 {
stmtIns, err = database.Prepare("INSERT INTO " + _TABLE_FAVORITE + "(userId, userName, tweetId, status, favDate, unfavDate, lastAction) VALUES( ?, ?, ?, ?, ?, ?, ?)")
} else {
stmtIns, err = database.Prepare("UPDATE " + _TABLE_FAVORITE + " SET userId = ?, userName = ?, tweetId = ?, status = ?, favDate = ?, unfavDate = ?, lastAction = ? WHERE id = ?")
}
if err != nil {
return err
}
defer stmtIns.Close()
unfavDate := mysql.NullTime{Time: fav.UnfavDate, Valid: !fav.UnfavDate.IsZero()}
if fav.id == 0 {
_, err = stmtIns.Exec(fav.UserId, fav.UserName, fav.TweetId, fav.Status, fav.FavDate, unfavDate, time.Now())
} else {
_, err = stmtIns.Exec(fav.UserId, fav.UserName, fav.TweetId, fav.Status, fav.FavDate, unfavDate, fav.LastAction, fav.id)
}
return err
}
示例2: getCustomerPrice
func getCustomerPrice(personId int, productId int, number int) ([]*model.CustomerPrice, error) {
var conn *sql.DB
var stmt *sql.Stmt
var err error
if conn, err = db.Connect(); err != nil {
return nil, err
}
defer conn.Close()
_sql := "select * from customer_special_price where person_id = ? and product_id = ? order by create_time DESC limit ?"
if stmt, err = conn.Prepare(_sql); err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query(personId, productId, number)
if err != nil {
return nil, err
}
defer rows.Close()
models := []*model.CustomerPrice{}
for rows.Next() {
m := new(model.CustomerPrice)
var blackhole sql.NullInt64
err := rows.Scan(&m.Id, &m.PersonId, &m.ProductId, &m.Price, &m.CreateTime, &blackhole /*&m.LastUsedTime */)
if err != nil {
panic(err)
}
models = append(models, m)
}
return models, nil
}
示例3: GetPodsList
func (gkDbCon *GkDbConDef) GetPodsList() ([]DbPodDef, *gkerr.GkErrDef) {
var stmt *sql.Stmt
var err error
var dbPodsList []DbPodDef = make([]DbPodDef, 0, 0)
stmt, err = gkDbCon.sqlDb.Prepare("select id, pod_title from pods")
if err != nil {
return nil, gkerr.GenGkErr("sql.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
}
defer stmt.Close()
var rows *sql.Rows
rows, err = stmt.Query()
if err != nil {
return nil, gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
}
defer rows.Close()
for rows.Next() {
var dbPod DbPodDef
err = rows.Scan(&dbPod.Id, &dbPod.Title)
if err != nil {
return nil, gkerr.GenGkErr("rows.Scan"+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
}
dbPodsList = append(dbPodsList, dbPod)
}
return dbPodsList, nil
}
示例4: Delete
func (self *Params) Delete() (res sql.Result, err error) {
db, query := self.getWriteConnect()
sqls, val := query.Delete()
if debug_sql {
Debug.Println("delete ", sqls, val)
}
if OpenSyncDelete {
sqls = strings.Replace(sqls, "?", "%v", -1)
sqls = fmt.Sprintf(sqls, val...)
SqlSyncHook <- sqls
return nil, nil
} else {
var stmt *sql.Stmt
stmt, err = db.Prepare(sqls)
if err == nil {
defer stmt.Close()
} else {
Error.Println(err)
return
}
res, err = stmt.Exec(val...)
if err != nil {
Error.Println(err)
}
}
return
}
示例5: 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
}
示例6: SaveAcls
func SaveAcls(log *logging.Logger, tx *sql.Tx, section *common.Section, element string, acls []common.JSONFileAcl) error {
var acl common.JSONFileAcl
var stmt *sql.Stmt
var err error
var insert = "INSERT INTO acls" +
"(area, grace, dataset, element, name, type, perms)" +
" VALUES($1, $2, $3, $4, $5, $6, $7)"
stmt, err = tx.Prepare(insert)
if err != nil {
log.Debug("Failed prepare: " + err.Error())
return errorMsg(log, 1, "Cannot save ACLs for element "+section.Name)
}
for _, acl = range acls {
if acl.User != "" {
_, err = stmt.Exec(section.Name, section.Grace, section.Dataset, element,
acl.User, "user", acl.Mode)
} else {
_, err = stmt.Exec(section.Name, section.Grace, section.Dataset, element,
acl.Group, "group", acl.Mode)
}
if err != nil {
log.Debug("Failed execute: " + err.Error())
return errorMsg(log, 2, "Cannot save ACLs for element "+section.Name)
}
}
stmt.Close()
return nil
}
示例7: RawRead
func RawRead(b *B) {
var m *Model
var stmt *sql.Stmt
wrapExecute(b, func() {
var err error
initDB()
m = NewModel()
rawInsert(m)
stmt, err = raw.Prepare(rawSelectSQL)
if err != nil {
fmt.Println(err)
b.FailNow()
}
})
defer stmt.Close()
for i := 0; i < b.N; i++ {
var mout Model
err := stmt.QueryRow(m.Id).Scan(
&mout.Id,
&mout.Name,
&mout.Title,
&mout.Fax,
&mout.Web,
&mout.Age,
&mout.Right,
&mout.Counter,
)
if err != nil {
fmt.Println(err)
b.FailNow()
}
}
}
示例8: Persist
func (follow Follow) Persist() error {
var stmtIns *sql.Stmt
var err error
if follow.id == 0 {
stmtIns, err = database.Prepare("INSERT INTO " + _TABLE_FOLLOW + "(userId, userName, status, followDate, unfollowDate, lastAction) VALUES( ?, ?, ?, ?, ? ,?)")
} else {
stmtIns, err = database.Prepare("UPDATE " + _TABLE_FOLLOW + " SET userId = ?, userName = ?, status = ?, followDate = ?, unfollowDate = ?, lastAction = ? WHERE id = ?")
}
if err != nil {
return err
}
defer stmtIns.Close()
unfollowDate := mysql.NullTime{Time: follow.UnfollowDate, Valid: !follow.UnfollowDate.IsZero()}
if follow.id == 0 {
_, err = stmtIns.Exec(follow.UserId, follow.UserName, follow.Status, follow.FollowDate, unfollowDate, time.Now())
} else {
_, err = stmtIns.Exec(follow.UserId, follow.UserName, follow.Status, follow.FollowDate, unfollowDate, follow.LastAction, follow.id)
}
return err
}
示例9: sexDomain
func sexDomain(dom string) (sexAnswer bool, err error) {
var stmt *sql.Stmt
stmt, err = db.Prepare(`SELECT dom FROM domains WHERE dom = $1 AND sex = true LIMIT 1`)
if err != nil {
fmt.Printf("db.Prepare error: %v\n", err)
return false, err
}
var rows *sql.Rows
rows, err = stmt.Query(dom)
if err != nil {
fmt.Printf("stmt.Query error: %v\n", err)
return false, err
}
defer stmt.Close()
if rows.Next() {
var domain string
err = rows.Scan(&domain)
if err != nil {
fmt.Printf("rows.Scan error: %v\n", err)
return false, err
}
if configuration.Debug == "Yes" {
fmt.Println("domain => \"" + domain + "\"")
}
return true, err
}
return false, nil
}
示例10: execDelete
/* **
执行删除操作
-----------------------
param1: 要操作的数据库表
param2: 条件语句,当没有条件时填空
param3: 对应条件传的参数
return 成功返回nil,失败返回error
** */
func (self *MDB) execDelete(tableName, condition string, args ...interface{}) error {
if self == nil {
return errors.New("数据库句柄错误。")
}
if tableName == "" {
return errors.New("数据库表名不能为空。")
}
tx, err := self.Begin()
if err != nil {
return err
}
var stmt *sql.Stmt
if condition == "" || len(args) == 0 {
stmt, err = tx.Prepare(fmt.Sprintf("DELETE FROM %s", tableName))
} else {
stmt, err = tx.Prepare(fmt.Sprintf("DELETE FROM %s WHERE %s", tableName, condition))
}
if err != nil {
return err
}
if _, err = stmt.Exec(args...); err != nil {
return err
}
defer stmt.Close()
if err = tx.Commit(); err != nil {
// 回滚????用得着么
tx.Rollback()
return err
}
return nil
}
示例11: NextSequence
func (dbSeq *SequenceDB) NextSequence() (sequence uint64, err error) {
var stmt *sql.Stmt
stmt, err = dbSeq.db.Prepare(`REPLACE INTO sequence(stub) VALUES ("sequence")`)
if err != nil {
log.Printf("sequence db prepare error. %v", err)
return 0, err
}
defer stmt.Close()
var res sql.Result
res, err = stmt.Exec()
if err != nil {
log.Printf("sequence db replace into error. %v", err)
return 0, err
}
// 兼容LastInsertId方法的返回值
var lastID int64
lastID, err = res.LastInsertId()
if err != nil {
log.Printf("sequence db get LastInsertId error. %v", err)
return 0, err
} else {
sequence = uint64(lastID)
// mysql sequence will start at 1, we actually want it to be
// started at 0. :)
sequence -= 1
return sequence, nil
}
}
示例12: GetContextUser
// no error return if row is not found
func (gkDbCon *GkDbConDef) GetContextUser(userName string) (*DbContextUserDef, bool, *gkerr.GkErrDef) {
var stmt *sql.Stmt
var err error
var dbContextUser *DbContextUserDef = new(DbContextUserDef)
var rowFound bool
stmt, err = gkDbCon.sqlDb.Prepare("select context_users.id, context_users.last_position_x, context_users.last_position_y, context_users.last_position_z, context_users.last_pod from users, context_users where users.id = context_users.id and users.user_name = $1")
if err != nil {
return nil, false, gkerr.GenGkErr("sql.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
}
defer stmt.Close()
var rows *sql.Rows
rows, err = stmt.Query(userName)
if err != nil {
return nil, false, gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
}
defer rows.Close()
if rows.Next() {
err = rows.Scan(&dbContextUser.id, &dbContextUser.LastPositionX, &dbContextUser.LastPositionY, &dbContextUser.LastPositionZ, &dbContextUser.LastPod)
if err != nil {
return nil, false, gkerr.GenGkErr("rows.Scan"+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
}
rowFound = true
} else {
rowFound = false
}
return dbContextUser, rowFound, nil
}
示例13: GetUser
// return error if row not found
func (gkDbCon *GkDbConDef) GetUser(userName string) (*DbUserDef, *gkerr.GkErrDef) {
var stmt *sql.Stmt
var err error
var dbUser *DbUserDef = new(DbUserDef)
stmt, err = gkDbCon.sqlDb.Prepare("select id, user_name, password_hash, password_salt, email, account_creation_date, last_login_date from users where user_name = $1")
if err != nil {
return nil, gkerr.GenGkErr("sql.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
}
defer stmt.Close()
var rows *sql.Rows
rows, err = stmt.Query(userName)
if err != nil {
return nil, gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
}
defer rows.Close()
if rows.Next() {
err = rows.Scan(&dbUser.id, &dbUser.UserName, &dbUser.PasswordHash, &dbUser.PasswordSalt, &dbUser.Email, &dbUser.accountCreationDate, &dbUser.lastLoginDate)
if err != nil {
return nil, gkerr.GenGkErr("rows.Scan"+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
}
} else {
return nil, gkerr.GenGkErr("select users", nil, ERROR_ID_NO_ROWS_FOUND)
}
return dbUser, nil
}
示例14: AddNewUser
func (gkDbCon *GkDbConDef) AddNewUser(userName string, passwordHash string, passwordSalt string, email string) *gkerr.GkErrDef {
var stmt *sql.Stmt
var err error
var id int64
var gkErr *gkerr.GkErrDef
id, gkErr = gkDbCon.getNextUsersId()
if gkErr != nil {
return gkErr
}
stmt, err = gkDbCon.sqlDb.Prepare("insert into users (id, user_name, password_hash, password_salt, email, account_creation_date, last_login_date) values ($1, $2, $3, $4, $5, $6, $7)")
if err != nil {
return gkerr.GenGkErr("stmt.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
}
defer stmt.Close()
var accountCreationDate time.Time = time.Now()
var lastLoginDate time.Time = time.Now()
_, err = stmt.Exec(id, userName, passwordHash, passwordSalt, email, accountCreationDate, lastLoginDate)
if err != nil {
if isUniqueViolation(err) {
return gkerr.GenGkErr("stmt.Exec unique violation", err, ERROR_ID_UNIQUE_VIOLATION)
}
return gkerr.GenGkErr("stmt.Exec"+getDatabaseErrorMessage(err), err, ERROR_ID_EXECUTE)
}
return nil
}
示例15: SetEmailTaskDone
func SetEmailTaskDone(emailTask *EmailTask) bool {
var db *sql.DB
var stmt *sql.Stmt
var err error
LogError := func() {
seelog.Errorf("[SetEmailTaskDone Failed] [EmailTask : %v] [ERROR : %v]", *emailTask, err)
}
if db, err = GetDBConnection(); err != nil {
LogError()
return false
}
defer db.Close()
if stmt, err = db.Prepare("UPDATE `email_request` SET `is_done` = 1 WHERE `id` = ?"); err != nil {
LogError()
return false
}
defer stmt.Close()
if _, err = stmt.Exec(emailTask.Id); err != nil {
LogError()
return false
}
return true
}