dbx icon indicating copy to clipboard operation
dbx copied to clipboard

如何批量插入数据

Open zss007 opened this issue 4 years ago • 1 comments

如果是同时插入多条数据,有对应的api吗,比如 insertMulit 这种

zss007 avatar Jul 01 '20 09:07 zss007

方法1: dbx 100%支持原生,所以可以使用原生的方法来批量执行

rawInsertBaseSQL   = "INSERT INTO `model` (`name`, `title`, `fax`, `web`, `age`, `right`, `counter`) VALUES "
	rawInsertValuesSQL = "(?, ?, ?, ?, ?, ?, ?)"
	rawInsertSQL       = rawInsertBaseSQL + rawInsertValuesSQL
 
query := rawInsertBaseSQL + strings.Repeat(rawInsertValuesSQL+",", len(ms)-1) + rawInsertValuesSQL
		args := make([]interface{}, len(ms)*nFields)
		for j := range ms {
			offset := j * nFields
			args[offset+0] = ms[j].Name
			args[offset+1] = ms[j].Title
			args[offset+2] = ms[j].Fax
			args[offset+3] = ms[j].Web
			args[offset+4] = ms[j].Age
			args[offset+5] = ms[j].Right
			args[offset+6] = ms[j].Counter
		}
		_, _, err := database.Execute(query, args...)
		if err != nil {
			fmt.Println(err)
			b.FailNow()
		}


tietang avatar Aug 27 '21 07:08 tietang