数据库
首页 > 数据库> > go sqlit3使用一

go sqlit3使用一

作者:互联网

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
)

var jointNames = []string{"boom_yaw", "boom_pitch", "boom_stretch", "feed_roll", "feed_pitch", "feed_yaw", "feed_stretch",
	"vel_boom_yaw", "vel_boom_pitch", "vel_boom_stretch", "vel_feed_roll", "vel_feed_pitch", "vel_feed_yaw", "vel_feed_stretch"}
var ValveControl *valve

func main() {
A:=newValve()
	fmt.Println(A)

}

func newValve() *valve {
	ValveControl=&valve{
		joints: make(map[string]*pid),
	}
	newPos()
	return ValveControl
}

type valve struct {
	joints map[string]*pid
}

func newPos() *map[string]*pid {
	for _, name := range jointNames {
		ValveControl.joints[name] = newPid()

		pidParaDb(ValveControl.joints[name])
	}
	return &ValveControl.joints
}

type pid struct {
	Kp          float64
	Ki          float64
	Kd          float64
	err         float64
	errLast     float64
	errMax      float64
	integral    float64
	setAngle    float64
	actualAngle float64
	integralMax float64
	integralMin float64
	dMax        float64
	dMin        float64
}

func newPid() *pid  {
	return &pid{
		Kp:          0.1,
		Ki:          0.2,
		Kd:          0.3,
		err:         0.0,
		errLast:     0.0,
		errMax:      0.0,
		integral:    0.0,
		setAngle:    0.0,
		actualAngle: 0.0,
		integralMax: 0.0,
		integralMin: 0.0,
		dMax:        0.0,
		dMin:        0.0,
	}
}

//PID参数数据库存储
func pidParaDb(pos *pid) {
	//打开数据库,如果不存在,则创建
	db, err := sql.Open("sqlite3", "./pid.db")
	checkErr(err)

	//创建表
	sql_table := `
   CREATE TABLE IF NOT EXISTS pid(
       uid INTEGER PRIMARY KEY AUTOINCREMENT,
       Kp DOUBLE NULL,
       Ki DOUBLE NULL,
       Kd DOUBLE NULL,
       errMax DOUBLE NULL,
       integralMax DOUBLE NULL,
       uMax DOUBLE NULL,
       uMin DOUBLE NULL
   );
  
	`
	db.Exec(sql_table)

	//执行Sql语句,插入数据
	stmt, err := db.Prepare("INSERT INTO pid(Kp, Ki, Kd,errMax,integralMax,uMax,uMin) values(?,?,?,?,?,?,?)")
	checkErr(err)
	//res, err := stmt.Exec(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7)
	res, err := stmt.Exec(pos.Kp, pos.Ki, pos.Kd, pos.errMax, pos.integralMax, pos.dMax, pos.dMin)
	fmt.Println(res)
	checkErr(err)
	id, err := res.LastInsertId() //返回新增的id号
	checkErr(err)

	//更新数据,可以更新多个参数
	fmt.Println("更新数据 ")
	stmt, err = db.Prepare("update pid set Ki=?,Kd=? where uid=?")
	checkErr(err)
	res, err = stmt.Exec(0.03,0.11, id) //将新增的id的username修改为astaxieupdate
	checkErr(err)
	affect, err := res.RowsAffected()
	checkErr(err)
	fmt.Println(affect, "条数据被更新")

	//查询sql语句,查询数据
	rows, err := db.Query("SELECT * FROM pid")
	checkErr(err)
	var uid int
	var Kp, Ki, Kd, errMax, integralMax, dMax, dMin float64

	//删除数据;
	//fmt.Print("删除数据,ID=")
	//stmt, err = db.Prepare("delete from userinfo where uid=?")
	//checkErr(err)
	//res, err = stmt.Exec(1) //将想删除的id输入进去就可以删除输入的id
	//checkErr(err)
	//affect, err := res.RowsAffected() //几条数据受影响:返回int64类型数据
	//checkErr(err)
	//fmt.Println(uid)
	//fmt.Println(affect, "条数据被删除")

	for rows.Next() {
		err = rows.Scan(&uid, &Kp, &Ki, &Kd, &errMax, &integralMax, &dMax, &dMin)
		checkErr(err)
		fmt.Println(uid, ":", Kp, Ki, Kd, errMax, integralMax, dMax, dMin)
	}

	rows.Close()
}

//异常处理
func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

标签:checkErr,err,float64,0.0,fmt,pid,sqlit3,使用,go
来源: https://blog.csdn.net/chenqianghqu/article/details/121502032