# \[ANN\] SQLdf - SQL for Julia DataFrames

**URL:** https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270
**Category:** Package Announcements
**Tags:** package, announcement
**Created:** [July 8, 2021, 11:25am UTC](https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270 "2021-07-08T11:25:58Z")
**Posts on this page:** 7
**Page:** 2

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [July 11, 2021, 7:01pm UTC](https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270/21 "2021-07-11T19:01:56Z")

</div>

Ok, here’s my proposed code:

```julia
module SQLiteDF
using SQLite
export @sqldf

macro sqldf(a...)
    db = gensym()
    return esc(quote
        $db = SQLite.DB();
        for sym in $(a[1:end-1])
            SQLite.load!(eval(sym),$db,String(sym))
        end;
        SQLite.DBInterface.execute($db,$(a[end]))
    end)
end

end

```

I haven’t actually turned it into a package yet that I can load by “using” but if I test it as follows, it seems to work:

```julia
using Pkg
Pkg.activate(".")
using DataFrames

include("SQLiteDF.jl")

a = DataFrame(foo=[1,2,3])
b = DataFrame(foo=[1,2,3],bar=[4,5,6])

print(DataFrame(SQLiteDF.@sqldf(a,b,"select a.*,b.bar from a join b on a.foo=b.foo")))

3×2 DataFrame
 Row │ foo bar   
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 4
   2 │ 2 5
   3 │ 3 6

```

---

<div class="post-metadata">

### Author: ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)
#### Post date: [July 11, 2021, 8:05pm UTC](https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270/22 "2021-07-11T20:05:13Z")

</div>

Cool! I’ll try to integrate SQLite with the parser I have, thanks! 🙂

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [July 11, 2021, 8:15pm UTC](https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270/23 "2021-07-11T20:15:04Z")

</div>

You could offer two versions, one where the user provides explicit table name symbols. One where you just provide a SQL query and it parses, extracts table names then calls the explicit version

---

<div class="post-metadata">

### Author: ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)
#### Post date: [July 11, 2021, 9:00pm UTC](https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270/24 "2021-07-11T21:00:24Z")

</div>

With all due respects but I just don’t think passing tables names is a good idea, I understand you may believe parsing is a “design mistake” however I just happen to believe it is a good design.

Nonetheless I accept that I might be wrong, that’s why perhaps it is better at this point if you develop a package passing table names for you and for those that like you think that this is a good design, and on my side I’ll keep developing the parsing version in SQLdf for me and for those that like me prefer this approach. I hope this sounds good for everybody.

Good luck!

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [July 11, 2021, 9:13pm UTC](https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270/25 "2021-07-11T21:13:06Z")

</div>

No worries I’ll make my own package but I guess my point was if you want to use that code within your own implementation it would be fine.

My biggest concern about the SQL parser version is that it’s add a lot of technical debt and maintenance issues. I’m comfortable maintaining those few lines of macro but wouldn’t want the burden of maintaining a SQL parser myself.

---

<div class="post-metadata">

### Author: ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)
#### Post date: [July 11, 2021, 9:21pm UTC](https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270/26 "2021-07-11T21:21:41Z")

</div>

It’s all right, I’ll check the SQLite.jl documentation to see what’s new and what’s the best way to integrate it in SQLdf. Last year when I tried SQLite.jl I think I had some issues and I just moved to RCall/sqldf, now it seems it’s working better and I should have had a look at it before integrating SQLdf with RCall/sqldf.

All is good though, I will maintain the parser in my version, good luck again!

---

<div class="post-metadata">

### Author: ![arimeyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arimeyer/32/45526_2.png) [@arimeyer](https://discourse.julialang.org/u/arimeyer)
#### Post date: [January 2, 2023, 12:06am UTC](https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270/27 "2023-01-02T00:06:12Z")

</div>

Hi @viraltux , I’m learning DataFrames basics, and a SQL-ish translation layer immediately came to mind. I do see that you haven’t updated the code in awhile. Has there been much uptake or any efforts to standardize this or something similar? Cheers!

[Previous page](https://discourse.julialang.org/t/ann-sqldf-sql-for-julia-dataframes/64270.md?page=1)
