# Inserting into sqlite DB en masse?

**URL:** https://discourse.julialang.org/t/inserting-into-sqlite-db-en-masse/27777
**Category:** General Usage
**Created:** [August 20, 2019, 10:48pm UTC](https://discourse.julialang.org/t/inserting-into-sqlite-db-en-masse/27777 "2019-08-20T22:48:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [August 20, 2019, 10:48pm UTC](https://discourse.julialang.org/t/inserting-into-sqlite-db-en-masse/27777/1 "2019-08-20T22:48:21Z")

</div>

I’m currently entering rows one-at-a time using Stmt,bind!,execute! and the process is quite slow, even though there are only 3 columns in the table, and I have a lot of rows to insert.

I’ve been looking through the docs and I haven’t found a way to generate a more efficient formulation within Julia (\*). it looks as though the queries have to be performed one-at-a-time. is there a way to prepare a block of inserts ?

Thanks.

(\*) the most efficient formulation i’ve found so far is to generate a csv file and use csv import in sqlite.

---

<div class="post-metadata">

### Author: ![fborda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fborda/32/8654_2.png) [@fborda](https://discourse.julialang.org/u/fborda)
#### Post date: [August 21, 2019, 2:01am UTC](https://discourse.julialang.org/t/inserting-into-sqlite-db-en-masse/27777/2 "2019-08-21T02:01:47Z")

</div>

I don’t have practice with SQLite.jl, but looking at the documentation seems like an alternative which could be faster would be inserting it all in a temporary table and then copying it within SQLite.

```julia
SQLite.Load!(source, db, "table_temp", temp=true)
SQLite.Query(db, "INSERT INTO table SELECT * FROM table_temp")

```

You could also use SQLite’s UPSERT if you want to insert or update if it exists. In this case source could be a dataframe or an array of named tuples (anything that implements Tables.jl)

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [August 21, 2019, 3:55am UTC](https://discourse.julialang.org/t/inserting-into-sqlite-db-en-masse/27777/3 "2019-08-21T03:55:51Z")

</div>

The documentation for that call was sitting right under the Query call I must have skipped right over it when I was looking through.

I haven’t tried it but that’s certain to be much faster than one row at it me, and certainly much more convenient.

Thank you !

---

<div class="post-metadata">

### Author: ![fborda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fborda/32/8654_2.png) [@fborda](https://discourse.julialang.org/u/fborda)
#### Post date: [August 21, 2019, 4:08am UTC](https://discourse.julialang.org/t/inserting-into-sqlite-db-en-masse/27777/4 "2019-08-21T04:08:23Z")

</div>

Actually, I just tested and you can load! onto a table that already exists, so you don’t need the second step.
