编写操作数据库的函数
实现数据库必要的增删改查操作为接口搭建做准备
// 添加操作信息,向表OperationRecord(操作列表)添加
func Addoperation(id int, operation string) error {
db, err := Opensql()
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec("insert into OperationRecord (id,behaver,time) VALUES (?, ?, ?)", id, operation, time.Now().Unix())
if err != nil {
return err
}
return nil
}
// 获取:表OperationRecord(操作列表)的数据
func Getoperations(id int) ([]OperationRecord, error) {
db, err := Opensql()
if err != nil {
return nil, err
}
defer db.Close()
rows, err := db.Query("SELECT behavior, time FROM OperationRecord WHERE id = ?", id)
if err != nil {
return nil, err
}
defer rows.Close()
result := []OperationRecord{}
for rows.Next() {
p := &OperationRecord{ID: id}
err = rows.Scan(p.Behavior, p.Time)
if err != nil {
return result, err
}
result = append(result, *p)
}
if err := rows.Err(); err != nil {
return result, err
}
return result, nil
}
// user表------------------------------------------------------------------
// add:向user(注册列表)添加元素
func AddLogindata(logindata *Logindata) error {
db, err := Opensql()
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec("insert into user (name,password,Email) VALUES (?, ?, ?)", logindata.Name, logindata.Password, logindata.Email)
if err != nil {
return err
}
return nil
}
// ((id是操作用的,不是给用户登录用的))
// get:获取:向user(注册列表)获取id,登陆成功,获取id
func GetuserId(email string) (int, error) {
db, err := Opensql()
if err != nil {
return -1, err
}
defer db.Close()
id := -1
err = db.QueryRow("SELECT id FROM user WHERE Email = ?", email).Scan(&id)
if err != nil {
return -1, err
}
return id, nil
}
// get:获取:向user(注册列表)获取email(改密时查Email,不能由用户传入Email,)
func GetuserEmail(id int) (string, error) {
db, err := Opensql()
if err != nil {
return "", err
}
defer db.Close()
Email := ""
err = db.QueryRow("SELECT Email FROM user WHERE id = ?", id).Scan(&Email)
if err != nil {
return "", err
}
return Email, nil
}
// 查:验证user(注册列表)密码
func Verifypassword(id int, password string) error {
db, err := Opensql()
if err != nil {
return err
}
defer db.Close()
count := 0
err = db.QueryRow("SELECT COUNT(*) FROM user WHERE id = ? AND password = ? ", id, password).Scan(&count)
if err != nil {
return err
}
return nil
}
// 改:修改:user(注册列表)密码
func Changepassword(id int, newpassword string) error {
db, err := Opensql()
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec("UPDATE users SET password = ? WHERE id = ?", newpassword, id)
if err != nil {
return err
}
return nil
}
// -SignInlist--------------------------------------------------------------------
// 查:SignIn查询身份验证功能:传入SignInData
func SignInVerification(signInData SignInData) error {
// 连接MySQL数据库
db, err := Opensql()
if err != nil {
return err
}
defer db.Close()
// 查询身份认证码是否正确
var expirationTime int64 //储存过期时间
err = db.QueryRow("SELECT ExpirationTime FROM SignInList WHERE id = ? AND IDcode = ?", signInData.ID, signInData.IDCode).Scan(&expirationTime)
if err != nil {
return fmt.Errorf("非法访问")
}
// 验证身份认证码是否过期
if time.Now().Unix() > expirationTime {
return fmt.Errorf("身份验证过期,请重新登陆")
}
// 验证成功.更新身份认证码的过期时间
_, err = db.Exec("UPDATE SignInList SET ExpirationTime = ? WHERE id = ? AND IDcode = ?", time.Now().Unix()+30*60, signInData.ID, signInData.IDCode)
if err != nil {
return err
}
return nil
}
// 增:向登录列表插入信息
func InsertSignInData(data SignInData) error {
// 打开数据库连接
db, err := Opensql()
if err != nil {
return err
}
defer db.Close()
// 准备SQL语句
stmt, err := db.Prepare("REPLACE INTO SignInList (id, IDcode, ExpirationTime) VALUES (?, ?, ?)")
if err != nil {
return err
}
defer stmt.Close()
// 执行插入操作
result, err := stmt.Exec(data.ID, data.IDCode, time.Now().Unix()+30*60)
if err != nil {
return err
}
// 输出插入结果
_, err = result.RowsAffected()
if err != nil {
return err
}
return nil
}
// VerificationCode(验证码储存库)----------------------------------------------------------
// 查: 验证验证码
func Verify(email string, verification int) error {
// 连接MySQL数据库
db, err := Opensql()
if err != nil {
return err
}
defer db.Close()
// 查询验证码是否正确
var expirationTime int64 //储存过期时间
//代表已经验证过
db.Exec("UPDATE VerificationCode SET password = ? WHERE Email = ?", 1, email)
err = db.QueryRow("SELECT ExpirationTime , time FROM VerificationCode WHERE Email = ? AND Verification = ?", email, verification).Scan(&expirationTime)
if err != nil {
return fmt.Errorf("验证码错误")
}
// 验证身份认证码是否过期
if time.Now().Unix() > expirationTime {
return fmt.Errorf("过期")
}
return nil
}
// 增:验证码了列表插入信息
func InsertVerification(data EmailVerificationCode) error {
// 打开数据库连接
db, err := Opensql()
if err != nil {
return err
}
defer db.Close()
// 准备SQL语句
stmt, err := db.Prepare("REPLACE INTO VerificationCode (Email, Verification, ExpirationTime, time) VALUES (?, ?, ?, ?)")
if err != nil {
return err
}
defer stmt.Close()
// 执行插入操作
_, err = stmt.Exec(data.Email, data.Verification, time.Now().Unix()+5*60, 0)
if err != nil {
return err
}
return nil
}
// get:获取:验证次数,重发验证码0无,1有
func GetuVerificationtime(email string) (int, error) {
db, err := Opensql()
if err != nil {
return -1, err
}
defer db.Close()
t := -1
err = db.QueryRow("SELECT time FROM user WHERE Email = ?", email).Scan(&t)
if err != nil {
return -1, err
}
return t, nil
}
本文由 mdnice 多平台发布