batch insert
vagra opened this issue · comments
Yan Chen commented
INSERT INTO 'dirs' ('id', 'name', 'parent_id') VALUES
('1', 'data1', '0'),
('2', 'data2', '1'),
('3', 'data3', '2'),
('4', 'data4', '3');
dotsql can achieve this function?
for example:
-- name: batch-insert-dirs
INSERT INTO 'dirs' ('id', 'name', 'parent_id') VALUES ?;
var dirs []Dir = []Dir{}
...
dirs = append(dirs, dir)
...
_, err := dot.Exec(db, batch-insert-dirs, dirs)
Gustavo Chaín commented
I'm not sure, did you try?
Yan Chen commented
I tried it, it doesn't work.
Gustavo Chaín commented
dotsql can achieve this function?
Nope
Yan Chen commented
very good! using BEGIN and END for sqlite auto transaction. very fast!
func InsertFiles(disk_name string) {
DBExec(db, "begin")
for _, file := range files {
DBExec(db, "add-file",
file.id, file.parent_id, file.name, file.path, file.size, file.mod_time)
}
DBExec(db, "end")
}
dot.sql:
-- name: begin
BEGIN
-- name: end
END
-- name: add-file
INSERT INTO files (id, parent_id, name, path, size, mod_time) VALUES(?, ?, ?, ?, ?, ?);
Gustavo Chaín commented
Nicely done!