官网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数据库增删查改)的更多相关文章

  1. django models进行数据库增删查改

    在cmd 上运行 python manage.py shell   引入models的定义 from app.models import  myclass   ##先打这一行    ------这些是 ...

  2. YII数据库增删查改操作

    初学YII, 整理了一些YII数据库的相关操作,  共同学习,共同进步. 一.查询数据集合 //1.该方法是根据一个条件查询一个集合 $admin=Admin::model()->findAll ...

  3. SQL Server跨数据库 增删查改

    比如你在库A ,想查询库B的表.可以用 数据库名.架构名.表名的方式查询 select * from 数据库B.dbo.表1 也可以在存储过程中这样使用. 需要注意的是,如果使用这样的查询方式,你必须 ...

  4. flask框架中,利用数据库增删查改

    # 配置数据库app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:mysql@127.0.0.1:3306/booktest" ...

  5. Django框架model实现数据库增删查改

    1.创建Django工程 https://www.cnblogs.com/CK85/p/10159159.html 2.在model.py中配置生成表格的类对象. from django.db imp ...

  6. Django学习笔记009-django models进行数据库增删查改

    引入models的定义 from app.models import  myclass class  myclass(): aa =  models. CharField (max_length=No ...

  7. 【总结】C# Access 数据库 增删查改 的简单步骤

        引用集: using System.Data.OleDb; static string exePath = System.Environment.CurrentDirectory;//本程序所 ...

  8. laravel 数据库 - 增删查改

    //查询public function select(){ /** 数据表 CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...

  9. 分享一段ios数据库代码,包括对表的创建、升级、增删查改

    分享一段ios数据库代码.包括创建.升级.增删查改. 里面的那些类不必细究,主要是数据库的代码100%可用. 数据库升级部分,使用switch,没有break,低版本一次向高版本修改. // DB.h ...

随机推荐

  1. Dreamweaver基本操作

    1.站点管理 在网站设计前,我们需要先建立站点,再进行网站设计. 站点 作用:用来归纳一个网站上所有的网页.素材及他们之间的联系. 站点根文件夹命名规则:由数字.字母.下划线组成,数字不能在开头. 首 ...

  2. 取消Eclipse的js校验功能

    1 window>>preferences>>javascript>>validator>>Error/warnings 去掉 Enable Javas ...

  3. 编译boost.log模块遇到的一些问题

    线上日志用到的是日志库,在全局有一个锁,导致在高并发的时候,容易因为锁竞争问题导致时延.在某些情况下,会因为同一个用户,同时访问某个变量,导致读写冲突使线上服务整体core掉(考虑到请求的间隔,为了应 ...

  4. iOS学习之UINavigationController详解与使用(三)ToolBar

    1.显示Toolbar  在RootViewController.m的- (void)viewDidLoad方法中添加代码,这样Toobar就显示出来了. [cpp] view plaincopy [ ...

  5. objective-C: NSString应该用initWithFormat? 还是 stringWithFormat?

    今天在看书上的一段代码时,发现NSString实例化时,有时用的是initWithFormat方法,有时用的是stringWithFormat,到底应该如何选择呢? 区别: 1.initWithFor ...

  6. Junit Test 的时候出错java.lang.IllegalStateException: Failed to load ApplicationContext

    问题原因 JDK1.8    spring版本3.2.0RELEASE JDK和spring版本不兼容 解决方法 1.降低JDK版本到1.7 2.将spring的版本升级到4.0.0RELEASE或者 ...

  7. 【原】Coursera—Andrew Ng机器学习—Week 10 习题—大规模机器学习

    [1]大规模数据 [2]随机梯度下降 [3]小批量梯度下降 [4]随机梯度下降的收敛 Answer:BD A 错误.学习率太小,算法容易很慢 B 正确.学习率小,效果更好 C 错误.应该是确定阈值吧 ...

  8. 实现二叉树的基本操作(Java版)

    近期研究了一下二叉树,试着用Java语言实现了二叉树的基本操作,下面分享一下实现代码: package com.sf.test; import java.util.ArrayDeque; import ...

  9. Python基础:Python数据类型及逻辑判断语句

    Python代码需要严谨的缩进 # 导包 import random # ********************输入输出***************** # 输出 print("hell ...

  10. ios加载本地html

    UIWebView加载工程本地网页与本地图片 - (void)viewDidLoad { [super viewDidLoad]; NSString *filePath = [[NSBundle ma ...