VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

附录2:调用示例

import (
	"fmt"
	"os"
	"reflect"
	"unsafe"
	_ "pq"
	"github.com/go-xorm/xorm"
)

const(
	host     =    "127.0.0.1"
	port     =    "5432"
	user     =    "vbadmin"
	password =    "Vbase@admin"
	dbname   =    "vastbase"
)

type Student struct{
	Id int64
	Name string
}

var engine *xorm.Engine

func main(){
	var err error
	Info := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",host,port,user,password,dbname)
	engine,err = xorm.NewEngine("postgres",Info)
	if err != nil{
		fmt.Println(err)
	}
 #创建表
	engine.Sync2(new(Student))
	temp := new(Student)
	temp.Id=1
	temp.Name="xiaohong"
#插入数据
	engine.Insert(temp)
	engine.Exec("insert into Student values(2,?),","xiaoli")
#更新数据
	temp.Name="xiaoming"
	engine.ID(1).Update(temp)
#查询数据Get方法
	student := new(Student)
	engine.ID(1).Get(student)
	fmt.Println(student.Name)
#查询数据Query方法
	engine.Query("select * from Student")
temp := make([]Student,0)
rc := engine.Find(&temp)
	engine.ID(1).Delete(student)
}