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
using Pkg
Pkg.add( "SQLite" )
using SQLite
db = SQLite.DB( "class" )
SQLite.tables(db)
|
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
using SQLite
db = SQLite.DB( "class" )
SQLite.execute(db, "CREATE TABLE IF NOT EXISTS Student(Roll_no REAL,
Name TEXT)")
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
using SQLite
db = SQLite.DB( "class" )
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
using SQLite
db = SQLite.DB( "class" )
SQLite.execute(db,"CREATE TABLE IF NOT EXISTS Student(Roll_no REAL,
Name TEXT)")
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
using SQLite
db = SQLite.DB( "class" )
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
using SQLite
db = SQLite.DB( "class" )
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 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
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,
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
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
Writing Data in Tabular form to Files in Julia
Julia is a high level, high performance, dynamic programming language which allows users to load, save, and manipulate data in various types of files for data science, analysis, and machine learning purposes. Tabular data is data that has a structure of a table and it can be easily written into vari
4 min read
String concatenation in Julia
String concatenation in Julia is a way of appending two or more strings into a single string whether it is character by character or using some special characters end to end. There are many ways to perform string concatenation. Example: Input: str1 = 'Geeks' str2 = 'for' str3 = 'Geeks' Output: 'Geek
2 min read
Date() function in Julia with Examples
Date() function in Julia works like a Constructors. The date can be constructed by Period types or integer by parsing the given period and integer. By default, it returns 0001-01-01. The first four digits represent the year, the next two digits represent the month and last two digits represent the d
2 min read