1 回答
TA贡献1856条经验 获得超11个赞
就像是:
func LoadObject(sql string, id int, dest ...interface{}) error {
db := tools.OpenDB()
defer db.Close()
stmt, err := db.Prepare(sql)
if err != nil {
log.Panic(err)
}
defer stmt.Close()
return stmt.QueryRow(id).Scan(dest)
}
// Loads the company from the database with the given id.
func LoadCompany(id int) (c Company, err error) {
err = LoadObject("SELECT * FROM companies WHERE id = ?", &c.id,
&c.FullName, &c.Name, &c.History, &c.Overview, &c.Est, &c.Phone, &c.Website, &c.Email)
return
}
请注意,我还没有编译此代码,但希望它足以给您一个想法。
几点建议:
sql.DB
在程序启动时创建实例在 SQL 语句中显式指定列顺序 (
select full_name, history, .... from companies ....
)
- 1 回答
- 0 关注
- 145 浏览
添加回答
举报