gorm如何一次插入多条数据?

Created on 25 Oct 2016  ·  10Comments  ·  Source: go-gorm/gorm

db.create([]user).err会出现错误。应该如何批量插入数据

Most helpful comment

@lpckkx 可以自定义表名
func (s 结构体)TableName() string {
return "表名"
}

All 10 comments

批量插入数据需要自己写sql。或者这样可以提高速度:

tx := DB.Begin()
tx.create(&user)
tx.commit()

这样开销太大。我插入100条数据这样插我要循环100次。。这样不行的。。。我现在直接sql插入的。。有没有什么方法可以不写sql呢

请问下连接mysql时候,执行操作,表名最后必须加上s才正常 要不然就报找不到表,是什么原因呢?

@lpckkx 可以自定义表名
func (s 结构体)TableName() string {
return "表名"
}

/*
用户
*/
type loguser struct {
ID int gorm:"primary_key"
name string
}

var user loguser

//1:first
db.First(&user)

fmt.Printf("user.name=%2s", user.name)

获取不到name的值 请问是哪里不对吗? 数据库 字段和接口构体一样 ,

非常感谢~

@owengaozhen 麻烦给看下,上面的问题 谢谢哈

@lpckkx 结构体name要大写

@owengaozhen
你批量插入怎么解决的呀?手写吗

我建了个群,我们一起讨论,287376123

Any solutions yet ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

littletwolee picture littletwolee  ·  3Comments

superwf picture superwf  ·  3Comments

hypertornado picture hypertornado  ·  3Comments

pjebs picture pjebs  ·  3Comments

corvinusy picture corvinusy  ·  3Comments