Working with Databases in Julia
Last Updated :
05 Sep, 2020
There are several ways through which data handling can be performed in Julia. Julia can be connected to a lot of databases whose connectors directly connect to Database Independent Interface (DBI) packages such as MySQL, SQLite, PostgreSQL, etc. These can be used for firing queries and obtaining the required output. In this article, we’ll discuss SQLite as a reference to connect with Julia, creating, dropping, inserting, updating, and querying the table using Julia.
Connecting SQLite with Julia
To connect Julia with SQLite, a package named SQLite is imported into the current session. It needs to be ensured that the SQLite server is running. The following command can be used to do the same:
using SQLite
To connect to a specific database, SQLiteDB() function and SQLite.DB() function can be used for Julia Version 3 and Julia Version 4 respectively.
For Julia Version 3:
db = SQLiteDB("dbname.sqlite")
For Julia Version 4:
db = SQLite.DB("dbname.sqlite")
After building a successful connection, queries can be executed using query() function and SQLite.query() function for Julia Version 3 and Julia Version 4 respectively.
For Julia Version 3:
query(db, "A SQL query")
For Julia Version 4:
SQLite.query(db, "A SQL query")
Install and import SQLite Package
SQLite Package in Julia can be installed using the function Pkg.add(). For importing it, 'using' keyword can be used followed by the package name.
Example:
Julia
# Install SQLite Package
using Pkg
Pkg.add("SQLite")
# Import SQLite
using SQLite
# Connect to the database(class)
db = SQLite.DB("class")
# Show tables in the database
SQLite.tables(db) # Empty Database with no tables
Output:

Create Tables in SQLite Using Julia
Tables in SQLite can be created using function execute() in Julia. This function executes the query on the provided database by taking the database connection object and the 'CREATE TABLE' SQL query to be fired as parameters.
Syntax:
SQLite.execute(db,query)
Parameter values:
db: represents the database connection object
query: represents the SQL query to be fired on the database
Julia
# Import SQLite
using SQLite
# Connect to the database(class)
db = SQLite.DB("class")
# Create a Table(Student)
SQLite.execute(db, "CREATE TABLE IF NOT EXISTS Student(Roll_no REAL,
Name TEXT)")
# Show tables in the database
SQLite.tables(db)
Output:

Drop Tables in SQLite Using Julia
Tables in SQLite can be dropped using function execute() in Julia. This function executes the query on the provided database by taking the database connection object and the 'DROP TABLE' SQL query to be fired as parameters.
Example:
Julia
# Import SQLite
using SQLite
# Connect to the database(class)
db = SQLite.DB("class")
# Drop Table(Student)
SQLite.execute(db, "DROP TABLE Student")
Output:

Insert into Table in SQLite Using Julia
Records can be inserted into a table in SQLite using function execute() in Julia. This function executes the query on the provided database by taking the database connection object and the 'INSERT INTO' SQL query to be fired as parameters.
Example:
Julia
# Import SQLite
using SQLite
# Connect to the database(class)
db = SQLite.DB("class")
# Create a Table(Student)
SQLite.execute(db,"CREATE TABLE IF NOT EXISTS Student(Roll_no REAL,
Name TEXT)")
# Insert data into Table
SQLite.execute(db, "INSERT INTO Student VALUES('Harry', 1)")
SQLite.execute(db, "INSERT INTO Student VALUES('Peter', 2)")
SQLite.execute(db, "INSERT INTO Student VALUES('Katy', 3)")
SQLite.execute(db, "INSERT INTO Student VALUES('Mia', 4)")
Output:
Database Content:

Updating a Table in SQLite Using Julia
Records can be updated into a table in SQLite using function execute() in Julia. This function executes the query on the provided database by taking the database connection object and the 'UPDATE' SQL query to be fired as parameters.
Example:
Julia
# Import SQLite
using SQLite
# Connect to the database(class)
db = SQLite.DB("class")
# Update data present in the table(Student)
SQLite.execute(db,"UPDATE Student SET Name = 'Simon' WHERE Roll_no = 1")
Output:
Database Content:

Querying a Table in SQLite Using Julia
Queries can be fired on a table in SQLite using function execute() in Julia. This function executes the query on the provided database by taking the database connection object and the 'SELECT' SQL query to be fired as parameters.
Example:
Julia
# Import SQLite
using SQLite
# Connect to the database(class)
db = SQLite.DB("class")
# Fire queries on the given Table(Student)
SQLite.execute(db, "SELECT * from Student WHERE Name='Simon'")
Output:
Database Content:
Similar Reads
Working with DataFrames in Julia
A Data frame is a two-dimensional data structure that resembles a table, where the columns represent variables and rows contain values for those variables. It is mutable and can hold various data types. Julia is a high performance, dynamic programming language which has a high-level syntax. The Data
7 min read
Working with CSV Files in Julia
CSV file (Comma-separated values file) is a plain text file that uses commas to separate values and fields. It is majorly used to store data in the form of tables or spreadsheets. Each row of a table or spreadsheet is a record filled with data that belongs to n fields (or Columns). It is used to imp
4 min read
Working with Date and Time in Julia
Julia provides a library to use Dates for dealing with date and time. The Dates module comes inbuilt with the Julia we just need to import it in order to use it. Now we need to prefix every function with an explicit type Dates, e.g Dates.Date. If you donât want to prefix on each function just add us
5 min read
Working with Text Files in Julia
Julia is Programming Language that is fast and dynamic in nature (most suitable for performing numerical and scientific applications) and it is optionally typed (the rich language of descriptive type and type declarations which is used to solidify our program) and general-purpose and open-sourced. J
4 min read
Working with Excel Files in Julia
Julia is a high-level open-source programming language meaning that its source is freely available. It is a language that is used to perform operations in scientific computing. Julia is used for statistical computations and data analysis. Julia provides its users with some pre-defined functions and
7 min read
Sorting of Arrays in Julia
The process of arranging the given data in a particular order or manner depends on the output. It is storing of data in sorted order. Sorting can be done in two ways i.e in ascending and descending order. Sorting can be performed using sorting algorithm or sorting functions. So, in sorting the progr
8 min read
Reshaping a Data Frame in Julia
DataFrame is a kind of Data Structure that holds the array of data in a tabular arrangement. We are familiar with the data frame objects and packages in python, which includes pandas, matplotlib so on, and so forth. Exactly with the equivalent approach in Julia, we use pandas.jl which operates as an
5 min read
Data Munging in Julia
Data Analysis is a process using which the raw data is cleansed, transformed, and modeled in a useful way to utilize it for making decisions in business or for discovering some information. The raw data cannot be used as it is. There will be many flaws like missing values in the datasets. Moreover,
9 min read
Handling Missing Data in Julia
Nowadays, one of the common problem of big data is to analyse the missing values in that data. Missing values can lead to some major prediction error which is not good for any business point of view. So when we encounter any missing data we have to apply different techniques to deal with missing val
4 min read
Sorting contents of a Data Frame in Julia
Sorting is a technique of storing data in sorted order. Sorting can be performed using sorting algorithms or sorting functions. to sort in a particular dataframe then extracting its values and applying the sorting function would easily sort the contents of a particular dataframe. Julia provides vari
6 min read