添加数据
Insert() 和 InsertAll()
可以新增一条或多条数据,注意第二个参数可选填,时间的字段名也可以通过全局去配置
添加一条数据 Insert()
- @param
data数据, 可以通过map也可以通过结构体传 - @param
options设置选项, 选填可不传tg.InsertOption{} - -------@param
Debug是否打印最终执行的SQL语句,默认不打印 - -------@param
AutoTime是否开启自动时间戳,默认不开启 - -------@param
CreateTime更新时间字段名,默认 create_time - -------@param
UpdateTime更新时间字段名,默认 update_time
id, err := tg.Db("user").Insert(map[string]interface{}{
"name", "张三",
"age", 18,
}, tg.InsertOption{ AutoTime: tgutl.PtrBool(true) })id, err := tg.Db("user").Insert(map[string]interface{}{
"name", "张三",
"age", 18,
}, tg.InsertOption{ AutoTime: tgutl.PtrBool(true) })最终的SQL语句为:
INSERT INTO user (name, age, create_time, update_time) VALUES ('张三', 18, '2023-12-12 00:00:00', '2023-12-12 00:00:00')INSERT INTO user (name, age, create_time, update_time) VALUES ('张三', 18, '2023-12-12 00:00:00', '2023-12-12 00:00:00')添加多条数据 InsertAll()
- @param
data数据, 可以通过map也可以通过结构体传 - @param
options设置选项, 选填可不传tg.InsertAllOption{} - -------@param
Debug是否打印最终执行的SQL语句,默认不打印 - -------@param
AutoTime是否开启自动时间戳,默认不开启 - -------@param
CreateTime更新时间字段名,默认 create_time - -------@param
UpdateTime更新时间字段名,默认 update_time
var user []map[string]interface{}
user = append(user, map[string]{}{
"name": "张三",
"age": 18,
}, map[string]{}{
"name": "李四",
"age": 20,
})
err := tg.Db("user").InsertAll(user)var user []map[string]interface{}
user = append(user, map[string]{}{
"name": "张三",
"age": 18,
}, map[string]{}{
"name": "李四",
"age": 20,
})
err := tg.Db("user").InsertAll(user)最终的SQL语句为:
INSERT INTO user (name, age) VALUES ('张三', 18), ('李四', 20)INSERT INTO user (name, age) VALUES ('张三', 18), ('李四', 20)