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 ...
随机推荐
- PHP - 脚本退出(包括异常退出),执行指定代码
之前做聊天室的时候有那么个需求就是当用户异常断线的时候就应该清除她的在线状态.因为当时对于flush不够了解,尝试了各种办法,好像都没办法在我们开发机上面执行相应的代码.后来知道是flush的原因.我 ...
- 将maven打包为一个jar(可以体外加入jar)
使用 maven-compiler-plugin插件, 在maven的pom的<build></build>标签中上加入 <build> <plugins&g ...
- springboot启动过程(3)-refresh方法
1 springboot在启动的时候,会调用run方法,创建环境设置spring容器,其中包含refresh方法,完成配置类解析,各种beanFactoryPostProcess和beanPostP ...
- 【BZOJ 2120】数颜色【分块/莫队】
题意 给出n个数字和m个操作.操作有两种.1:查询区间[l,r]内不同种类得数字个数.2: 将下标为p得数字修改为v 分析 如果不是修改操作的话,用莫队贼简单就可以水过,但是因为带了修改就有一些麻烦了 ...
- 前向渲染路径细节 Forward Rendering Path Details
正向渲染路径细节 Forward Rendering Path Details Forward Rendering path renders each object in one or more pa ...
- [Unity Shader笔记]渲染路径--Forward渲染路径
[Unity Shader笔记]渲染路径--Forward渲染路径 (2014-04-22 20:08:25) 转载▼ 标签: shader unity renderingpath forward 游 ...
- spring4-3-AOP-AspectJ注解-01-简单使用
1.引入类库 <dependency> <groupId>org.springframework</groupId> <artifactId>sprin ...
- servlet介绍
1.首先说Servlet API:servlet的命名:server+applet Servlet的框架是由两个Java包组成的:javax.servlet与javax.servlet.http. 在 ...
- 4.4.3 Java中的指针:Unsafe类
java多线程编程的无锁CAS底层都是通过 Unsafe进行操作的:源码如下 public final boolean compareAndSet(int expect, int update) { ...
- 部分类Partial
Partial告诉编译器,一个类,结构,接口的定义源代码可能要分散到一个或者多个源文件中. 在下面的情况下用Partial类型: (1) 类型特别大,不宜放在一个文件中实现.(2) 一个类型中的一部分 ...