go iris xorm包使用(sqlite3数据库增删查改)
官网https://studyiris.com/example/orm/xorm.html例子,稍做修改
1、我是win64,但没有遇到mingw问题,应该是之前安装过gcc环境,参考:测试一下robotgo自动化操作,顺便解决了原来的mingw版本中只有gcc,没有g++的问题
2、将其中的字段名、字段内容改为中文,并按id访问数据表中的行,没遇到乱码问题,很好。
代码如下:
//包主显示如何在您的Web应用程序中使用orm
//它只是插入一列并选择第一列。
package main import (
"time" "github.com/go-xorm/xorm"
"github.com/kataras/iris"
_ "github.com/mattn/go-sqlite3"
) /*
go get -u github.com/mattn/go-sqlite3
go get -u github.com/go-xorm/xorm
如果您使用的是win64并且无法安装go-sqlite3:
1.下载:https://sourceforge.net/projects/mingw-w64/files/latest/download
2.选择“x86_x64”和“posix”
3.添加C:\Program Files\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-rev1\mingw64\bin
到你的PATH env变量。
手册: http://xorm.io/docs/
*/
//User是我们的用户表结构。
type User struct {
ID int64 // xorm默认自动递增
Version string `xorm:"varchar(200)"`
Salt string
A用户名 string
Password string `xorm:"varchar(200)"`
Languages string `xorm:"varchar(200)"`
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
} func main() {
app := iris.New()
orm, err := xorm.NewEngine("sqlite3", "./test.db")
if err != nil {
app.Logger().Fatalf("orm failed to initialized: %v", err)
}
iris.RegisterOnInterrupt(func() {
orm.Close()
})
err = orm.Sync2(new(User))
if err != nil {
app.Logger().Fatalf("orm failed to initialized User table: %v", err)
}
app.Get("/insert", func(ctx iris.Context) {
user := &User{A用户名: "大大", Salt: "hash---", Password: "hashed", CreatedAt: time.Now(), UpdatedAt: time.Now()}
orm.Insert(user)
ctx.Writef("user inserted: %#v", user)
})
app.Get("/get/{id:int}", func(ctx iris.Context) {
id, _ := ctx.Params().GetInt("id")
//int到int64
id64 := int64(id)
ctx.Writef("id is %#v", id64)
user := User{ID: id64}
if ok, _ := orm.Get(&user); ok {
ctx.Writef("user found: %#v", user)
}
})
app.Get("/delete", func(ctx iris.Context) {
user := User{ID: }
orm.Delete(user)
ctx.Writef("user delete: %#v", user)
})
app.Get("/update", func(ctx iris.Context) {
user := User{ID: , A用户名: "小小"}
orm.Update(user)
ctx.Writef("user update: %#v", user)
})
// http://localhost:8080/insert
// http://localhost:8080/get/数字
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}
增:先访问2次:http://localhost:8080/insert
查:http://localhost:8080/get/1 和 http://localhost:8080/get/2
删:http://localhost:8080/delete
改:http://localhost:8080/update
补充:推荐使用SQLiteStudio.exe(https://sqlitestudio.pl/)查看数据库结构。
另外,可参考 Golang xorm工具,根据数据库自动生成 go 代码
https://www.cnblogs.com/DaBing0806/p/6680748.html
http://gobook.io/read/github.com/go-xorm/manual-zh-CN/
https://www.kancloud.cn/xormplus/xorm/167077
https://www.cnblogs.com/guhao123/p/4159688.html
https://www.cnblogs.com/chuankang/p/8727316.html#_label1
go iris xorm包使用(sqlite3数据库增删查改)的更多相关文章
- django models进行数据库增删查改
在cmd 上运行 python manage.py shell 引入models的定义 from app.models import myclass ##先打这一行 ------这些是 ...
- YII数据库增删查改操作
初学YII, 整理了一些YII数据库的相关操作, 共同学习,共同进步. 一.查询数据集合 //1.该方法是根据一个条件查询一个集合 $admin=Admin::model()->findAll ...
- SQL Server跨数据库 增删查改
比如你在库A ,想查询库B的表.可以用 数据库名.架构名.表名的方式查询 select * from 数据库B.dbo.表1 也可以在存储过程中这样使用. 需要注意的是,如果使用这样的查询方式,你必须 ...
- flask框架中,利用数据库增删查改
# 配置数据库app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:mysql@127.0.0.1:3306/booktest" ...
- Django框架model实现数据库增删查改
1.创建Django工程 https://www.cnblogs.com/CK85/p/10159159.html 2.在model.py中配置生成表格的类对象. from django.db imp ...
- Django学习笔记009-django models进行数据库增删查改
引入models的定义 from app.models import myclass class myclass(): aa = models. CharField (max_length=No ...
- 【总结】C# Access 数据库 增删查改 的简单步骤
引用集: using System.Data.OleDb; static string exePath = System.Environment.CurrentDirectory;//本程序所 ...
- laravel 数据库 - 增删查改
//查询public function select(){ /** 数据表 CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...
- 分享一段ios数据库代码,包括对表的创建、升级、增删查改
分享一段ios数据库代码.包括创建.升级.增删查改. 里面的那些类不必细究,主要是数据库的代码100%可用. 数据库升级部分,使用switch,没有break,低版本一次向高版本修改. // DB.h ...
随机推荐
- JSTL中EL表达式无法直接取size的处理
jsp中使用${list.size }会编译成list.getSize()方法,并不能获取list的长度,因为程序回去找List对象中的getSize()方法,所以只能想别的办法, 一种方法是在后台程 ...
- Mysql双机热备实现数据库高可用
mysql双主热备,也称主主互备,目的是mysql数据库高可用,只支持双机,原因是mysql的复制是一主多从,但一个从服务器只能有一个主服务器. 双机热备的条件是双机mysql版本必须一致. 服务器分 ...
- 使用jquery修改表单的提交地址
基本思路: 通过使用jquery选择器得到对应表单的jquery对象,然后使用attr方法修改对应的action 示例程序一: 默认情况下,该表单会提交到page_one.html 点击button之 ...
- Mask_rcnn openpose realsense
cd /home/luo/Desktop/MyFile/Mask_RCNN_Openpose_Realsense python realsense_mask_openpose_2019032601.p ...
- 富文本编辑器和fastdfs的使用
宜立方商城的系统架构a) 功能介绍(项目架构,有哪些功能模块,这些功能模块如何实现?)b) 架构讲解工程搭建-后台工程c) 使用maven搭建工程(后台工程如何搭建?)d) 使用maven的tomca ...
- ubuntu下搭建android开发环境
注意: google可能被和谐了,那就修改hosts sudo vim /etc/hosts 增加: #Google主页 203.208.46.146 www.google.com #这行是为了方便打 ...
- code1006 等差数列
我绞尽脑汁想一个更好的算法,然而不能如愿,只好写一个n^3的了 很简单,就是暴力搜索(还好n<100) 先排序,然后循环i=1ton,j=i+1ton 把a[i]a[j]确定为等差数列开始的两个 ...
- Anaconda 安装和配置
Anaconda 安装和配置 1. Anaconda 安装 Anaconda说明及安装过程:Anaconda详细安装使用教程 2. Anaconda和Pip源修改 Anaconda源修改:打开Anac ...
- kalilinux-漏洞评估
Nessus\OpenVAS http://www.tenable.com/products/nessus/select-your-operating-system http://www.nessus ...
- Javascirpt 常见的误区
var foo = new Object(); var bar = new Object(); var map = new Object(); map[foo] = "foo"; ...