本文整理汇总了Golang中database/sql.Result类的典型用法代码### 示例。如果您正苦于以下问题:Golang Result类的具体用法?Golang Result怎么用?Golang Result使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。
在下文中一共展示了Result类的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。
示例1: NewOrUpdateUser
func NewOrUpdateUser(db *sql.DB, user *User) error {
var err error
var res sql.Result
if user.Id == "" {
res, err = db.Exec("INSERT INTO users(firstName, lastName) VALUES(?,?)", user.FirstName, user.LastName)
} else {
res, err = db.Exec("UPDATE users SET firstName=?, lastName=? WHERE id=?", user.FirstName, user.LastName, user.Id)
}
if err != nil {
return err
}
lastId, err := res.LastInsertId()
if err != nil {
return err
}
rowCnt, err := res.RowsAffected()
if err != nil {
return err
}
if rowCnt != 1 {
return errors.New(fmt.Sprintf("Row count (%s) and/or lastId (%s) are wrong.", lastId, rowCnt))
}
return nil
}
开发者ID:gpavlidi,项目名称:sample-go-website,代码行数:25,代码来源:user.go
示例2: Insert
// Insert 向指定table插入数据
func (this *builder) Insert(table string, model interface{}) int {
query := "insert into `" + table + "`"
value := reflect.ValueOf(model).Elem()
data := make(map[string]interface{})
mapStructToMap(value, data)
keys := " ("
values := " ("
params := make([]interface{}, 0, 0)
for k, v := range data {
keys += "`" + k + "`,"
values += "?,"
params = append(params, v)
}
query += keys[:len(keys)-1] + ") values"
query += values[:len(values)-1] + ")"
var result sql.Result
var err error
this.reset()
result, err = this.db.Exec(query, params...)
if err != nil {
return -1
}
var id int64
id, err = result.LastInsertId()
if err != nil {
return -1
}
return int(id)
}
开发者ID:narrowizard,项目名称:tinysql,代码行数:30,代码来源:builder.go
示例3: CreateTables
func (database *Database) CreateTables(tableConf *TableConf) {
db, err := sql.Open(database.Driver, database.dataSource)
if err != nil {
panic(err)
}
var result sql.Result
var auto_increment_syntax = "INT AUTO INCREMENT "
if database.IsPostgres() {
auto_increment_syntax = "SERIAL "
}
fmt.Printf("creating table '%s'... \n", tableConf.Name)
query := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (id %s PRIMARY KEY, %s varchar(255), %s varchar(255), created_at timestamp DEFAULT CURRENT_TIMESTAMP, updated_at timestamp DEFAULT CURRENT_TIMESTAMP)", tableConf.Name, auto_increment_syntax, tableConf.Hash, tableConf.Url)
result, err = db.Exec(query)
if err != nil {
panic(err)
}
result.RowsAffected()
fmt.Printf("creating table 'goahead_statistics'... \n")
query = fmt.Sprintf("CREATE TABLE IF NOT EXISTS goahead_statistics (id %s PRIMARY KEY, created_at timestamp DEFAULT CURRENT_TIMESTAMP)", auto_increment_syntax)
result, err = db.Exec(query)
if err != nil {
panic(err)
}
result.RowsAffected()
}
开发者ID:Zocprint,项目名称:go301,代码行数:27,代码来源:query.go
示例4: Update
//Update ...
func (vs VictualService) Update(victual *Victual) *Victual {
db, err := sql.Open("postgres", "host=localhost port=5432 user=pober dbname=pober sslmode=disable")
checkErr(err)
defer db.Close()
var statement *sql.Stmt
statement, err = db.Prepare("UPDATE dc.victual SET description = $1, brand = $2, source = $3, energyCal = $4, energyJoule = $5, fat = $6, saturatedFat = $7, carbohydrates = $8, sugars = $9, protein = $10, salt = $11, fiber = $12, barCode = $13, weightVolumeRatio = $14 WHERE ID = $1")
checkErr(err)
var result sql.Result
result, err = statement.Exec(victual.ID,
victual.Description,
victual.Brand,
victual.Source,
victual.EnergyCal,
victual.EnergyJoule,
victual.Fat,
victual.SaturatedFat,
victual.Carbohydrates,
victual.Sugars,
victual.Protein,
victual.Salt,
victual.Fiber,
victual.BarCode,
victual.WeightVolumeRatio)
checkErr(err)
var rowsAffected int64
rowsAffected, err = result.RowsAffected()
checkErr(err)
statement.Close()
if rowsAffected == 1 {
return victual
}
return nil
}
开发者ID:berka77,项目名称:diabetes-companion,代码行数:36,代码来源:victualservice.go
示例5: Insert
func (this *Config) Insert() error {
this.Recordtime = getNow()
var (
err error
id int64
res sql.Result
)
if this.Cat != nil {
tran := this.Cat.NewTransaction(DBTITLE, "Config.Insert")
defer func() {
if err != nil {
tran.SetStatus(err)
} else {
tran.SetStatus("0")
}
tran.Complete()
}()
}
o := orm.NewOrm()
res, err = o.Raw("INSERT INTO config(channelCode,`key`,value,recordTime)VALUES(?,?,?,?)", this.ChannelCode, this.Key, this.Value, this.Recordtime).Exec()
if err != nil {
return err
}
id, err = res.LastInsertId()
if err != nil {
return err
}
this.Id = id
return nil
}
开发者ID:chenbk85,项目名称:nephele,代码行数:30,代码来源:config.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng