# How to use results from SQLite select query in Julia script

**URL:** https://discourse.julialang.org/t/how-to-use-results-from-sqlite-select-query-in-julia-script/61512
**Category:** New to Julia
**Created:** [May 20, 2021, 12:25pm UTC](https://discourse.julialang.org/t/how-to-use-results-from-sqlite-select-query-in-julia-script/61512 "2021-05-20T12:25:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ScottRobertson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottrobertson/32/24801_2.png) [@ScottRobertson](https://discourse.julialang.org/u/ScottRobertson)
#### Post date: [May 20, 2021, 12:25pm UTC](https://discourse.julialang.org/t/how-to-use-results-from-sqlite-select-query-in-julia-script/61512/1 "2021-05-20T12:25:13Z")

</div>

SUMMARY: I need a simple collection of `select` query results such as Python’s sqlite3.connect.cursor.fetchall(). In Python if I do a `select` query on my place table for IDs and names I get [(18, “Wichita”), (9, “Topeka”), (233, "Kansas City)]. So I just do  
`places = cur.fetchall()`. To use the value “Topeka” I reference `places[1][1]`.

The rest of the story:

I’ve been using sqlite3 with Python for three years and it’s really easy. I hope to move to Julia because Python might bog down under large databases.

I can get `insert`, and `update` queries to work as well as everything else I’ve tried, but can’t find documentation that includes a simple example of how to use results from a `select` query in a Julia script. I can get a pretty table of results to print in REPL, I can save that as a variable name and if I print the variable I get another copy of the pretty table. The pretty table is for my eyes, I can’t use it in a script.

If I remove the DataFrame() call I get a collection of stuff that must mean something to somebody but according to [this source](https://github.com/JuliaDatabases/SQLite.jl/issues/212), this is just a pointer to the query results. I see that DataFrame() does collect the results but I don’t see a collection I can reference in my code.

The pretty table shows the correct results but I’m looking for a collection such as the list of tuples returned by Python’s fetchone() or fetchall(). I can’t believe no one else has asked this question. I am so very new to Julia that I must be deficient in my understanding.

Here’s the code that prints the pretty table which as far as I can see is useless except for my eyes.

```julia
using SQLite, DataFrames
results = DataFrame(DBInterface.execute(db, "SELECT * FROM place"))

```

This on the other hand prints the output shown below in the REPL:

`DBInterface.execute(db, "SELECT * FROM place")`

output:  
SQLite.Query(SQLite.Stmt(SQLite.DB(“test”), 2), Base.RefValue{Int32}(100), [:place\_id, :places], Type[Union{Missing, Int64}, Union{Missing, String}], Dict(:place\_id =\> 1, :places =\> 2))

Thanks.

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [May 20, 2021, 1:20pm UTC](https://discourse.julialang.org/t/how-to-use-results-from-sqlite-select-query-in-julia-script/61512/2 "2021-05-20T13:20:05Z")

</div>

The execute statement returns data using the [https://github.com/JuliaData/Tables.jl](https://github.com/JuliaData/Tables.jl) interface. This can be processed by multiple Julia packages, including DataFrames.jl.  
Putting the output into a DataFrame is probably a good idea if you fetch a significant amount of data and need to analyze / process it further.  
Alternatively, you can write the query results directly into a file, e.g. using [https://github.com/lungben/TableIO.jl](https://github.com/lungben/TableIO.jl) (shameless self promotion 😉 ).  
Or use the Tables.jl interface directly to get specific values, as described here: [Home · Tables.jl](https://tables.juliadata.org/stable/)

---

<div class="post-metadata">

### Author: ![jacobadenbaum](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jacobadenbaum](https://discourse.julialang.org/u/jacobadenbaum)
#### Post date: [May 20, 2021, 2:30pm UTC](https://discourse.julialang.org/t/how-to-use-results-from-sqlite-select-query-in-julia-script/61512/3 "2021-05-20T14:30:38Z")

</div>

You may find it useful to take a look at the documentation for the [DataFrames.jl](https://github.com/JuliaData/DataFrames.jl) package. This is in many ways the Julia equivalent of a pandas dataframe, although the usage is a little different.

You can access a particular column of the Dataframe with the dot-accessor notation. I.e,

```julia
using SQLite, DataFrames
results = DataFrame(DBInterface.execute(db, "SELECT col1, city FROM place"))
results.city[2]
# outputs "Topeka"

```

---

<div class="post-metadata">

### Author: ![ScottRobertson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottrobertson/32/24801_2.png) [@ScottRobertson](https://discourse.julialang.org/u/ScottRobertson)
#### Post date: [May 20, 2021, 2:43pm UTC](https://discourse.julialang.org/t/how-to-use-results-from-sqlite-select-query-in-julia-script/61512/4 "2021-05-20T14:43:09Z")

</div>

Thanks for the simple example. It not only answers my question and reassures me, but it points me in the right direction for further research.
