Answer the question
In order to leave comments, you need to log in
Insert NULL into DB?
There are such constructions:
DB.User.Create(database.User{Username: "юзернейм1", Password: "пароль"})
func (obj *UserObject) Create(user User) error {
if (User{}) == user {
return structEmptyErr()
}
var role = MayBeNullStr(user.Role)
fmt.Println(role)
var regIP = MayBeNullStr(user.RegIP)
var regAgent = MayBeNullStr(user.RegAgent)
var qu = fmt.Sprintf("insert into users (role, username, password, reg_ip, reg_agent) values ('%v', '%v', '%v', '%v', '%v') RETURNING id", role, user.Username, user.Password, regIP, regAgent)
result, err := obj.db.Exec(context.Background(), qu)
checkErr(err)
println(result)
return nil
}
func MayBeNullStr(str string) interface{} {
if len(str) < 1 {
return sql.NullString{}
}
return str
}
Answer the question
In order to leave comments, you need to log in
This is not how you can insert records into the database, there will be SQL injection.
var qu = fmt.Sprintf("insert into users (role, username, password, reg_ip, reg_agent) values ('%v', '%v', '%v', '%v', '%v') RETURNING id", role, user.Username, user.Password, regIP, regAgent)
result, err := obj.db.Exec(context.Background(), qu)
var qu = fmt.Sprintf("insert into users (role, username, password, reg_ip, reg_agent) values (?, ?, ?, ?, ?) RETURNING id", )
result, err := obj.db.Exec(context.Background(), qu, role, user.Username, user.Password, regIP, regAgent)
result, err := obj.db.Exec(context.Background(), qu, MayBeNullStr(role), MayBeNullStr(user.Username) и т.д.)
SELECT COALESCE(Username, '') AS Username
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question