官网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. 【309】◀▶ Windows 相关功能实现

    目录: 共享文件夹失败的解决方法 导 栅 添 1. 共享文件夹失败的解决方法 参考:解决“你没有权限访问,请与网络管理员联系” 参考:WIN7局域网文件共享设置方法 2. 导 在 3. 栅 栅 4. ...

  2. iOS学习之WebView的使用

    1.使用UIWebView加载网页 运行XCode 4.3,新建一个Single View Application,命名为WebViewDemo. 2.加载WebView 在ViewControlle ...

  3. 前端开发之JavaScript HTML DOM理论篇二

    主要内容: 1.HTML DOM元素 2.HTML DOM事件 一.DOM元素 主要操作有添加.删除和替换HTML元素 1.创建新的HTML元素  (1)方法一: appendChild() 追加 如 ...

  4. Ubuntu Server 12.04 LTS搭建SVN服务及修改端口

    采用了apache结合svn的方式. 首先安装apache.subversion.svn-apache sudo apt-get install apache2 sudo apt-get instal ...

  5. controller,service,repository,component注解的使用对比

    项目中的controller层使用@controller注解 @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使用了 ...

  6. GRUB使用说明

    从Red Hat Linux 7.2起,GRUB(GRand Unified Bootloader)取代LILO成为了默认的启动装载程序.相信LILO对于大家来说都是很熟悉的.这次Red Hat Li ...

  7. yii2.0 报错Cookievalidationkey Must Be Configured With A Secret Key

    'components' => [ 'request' => [ // !!! insert a secret key in the following (if it is empty) ...

  8. JMeter下载及安装配置完整版

    特别需要注意的时,jdk版本和jmeter版本匹配问题. Jdk1.8对应apache-jmeter-3.3 Jmeter下载及安装配置 本文是在win7环境下安装使用jmeter,jmeter可以运 ...

  9. 实践作业4:Web测试实践(小组作业)每日任务记录5

    (一)今日任务更新 本次小组作业均已完成! 本组文件最终pdf文件(文件稍大,请耐心等待加载):https://files.cnblogs.com/files/ruanshuo170204/Web测试 ...

  10. [GO]timer的应用

    package main import ( "time" "fmt" ) func main() { //创建一个定时器,设置时间为2s,2s后,往time.C ...