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 ...
随机推荐
- Python中的try...except...finally
Python的异常处理代码格式如下: try: // do something except Exception as e: // dual with exception finally: // fi ...
- nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c
首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sys ...
- 存储过程中使用事务和try catch
一.存储过程中使用事务的简单语法 在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 : Cre ...
- eclipse 使用 scons 编译的配置说明
eclipse版本: eclipse-cpp-kepler-SR1-win32.zip 创建项目必须选择“Makefile Project” 然后进入“Projects Properities” 先 ...
- SpringBoot集成freemarker和thymeleaf模板
1.在MAVEN工程POM.XML中引入依赖架包 <!-- 引入 freemarker 模板依赖 --> <dependency> <groupId>org.spr ...
- Golang 之 key-value LevelDB
时常会在应用中用到数据库功能,象 Key-Value 性质的.直接搬个 Redis,Mysql嫌大,好在有 LevelDB,直接编进应用中. 有关什么是 LevelDB 以及 LevelDB 的特性, ...
- Ubuntu 安装phpmyadmin 和配置
ubuntu 安装 phpmyadmin 两种 : 1: apt-get 安装 然后使用 已有的虚拟主机目录建立软连接 sudo apt-get install phpmyadmin sud ...
- bash 环境配置及脚本
bash是 Bourne Again Shell简称 ,从unix系统的sh发展而来 查看当前shellecho $SHELL查看系统支持的shellcat /etc/shells cd /binls ...
- es学习-java操作 2.4.0版本
package esjava; import org.elasticsearch.action.bulk.*;import org.elasticsearch.action.delete.Delete ...
- 设计模式15---Android 观察者模式(转载自:“http://blog.csdn.net/fangchongbory/article/details/7774044”)
/* * 观察者模式 * 定义对象间的一种一个(Subject)对多(Observer)的依赖关系,当一个对象的状态发送改变时,所以依赖于它的 * 对象都得到通知并被自动更新 * * 当然, ...