Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何批量插入数据 #1

Open
zss007 opened this issue Jul 1, 2020 · 1 comment
Open

如何批量插入数据 #1

zss007 opened this issue Jul 1, 2020 · 1 comment

Comments

@zss007
Copy link

zss007 commented Jul 1, 2020

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

@tietang
Copy link
Owner

tietang commented Aug 27, 2021

方法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()
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants