-
Book Overview & Buying
-
Table Of Contents
Julia Cookbook
By :
In this section, we will explain ways to handle data stored in databases: MySQL and PostgreSQL.
MySQL is an open source relational database. To be able to interact with your MySQL databases from Julia, the database server (along with the relevant Julia package) needs to be installed. Assuming that the database is already set up and the MySQL session is already up and running, install the MySQL bindings for Julia by directly cloning the repository:
Pkg.clone("https://github.com/JuliaComputing/MySQL.jl")
PostgreSQL is an open source object relational database. Similar to the MySQL setup, the server of the PostgreSQL database should be up and running with a session. Now, install the PostgreSQL bindings for Julia by following the given instructions:
DBI package. The DBI package is a database-independent API that complies with almost all database drivers.DBI package from Julia can be installed by directly cloning it from its repository using the following statement:Pkg.clone("https://github.com/JuliaDB/DBI.jl")
PostgreSQL library by directly cloning the library's repository using the following statement:Pkg.clone("https://github.com/JuliaDB/PostgreSQL.jl")

Julia package by running the following standard package installation command:Pkg.add("SQLite")
Here, you will learn about connecting to databases and executing queries to manipulate and analyze data. You will also learn about the various protocols and libraries in Julia that will help you interact with databases.
A MySQL database can be connected by a simple command that takes in the host, username, password, and database name as parameters. Let's take a look at the following steps:
MySQL package:using MySQL
conn = mysql_connect(host, user_name, password, dbname)
query = """ CREATE TABLE Student ( ID INT NOT NULL AUTO_INCREMENT, Name VARCHAR(255), Attendance FLOAT, JoinDate DATE, Enrolments INT, PRIMARY KEY (ID) );"""
response = mysql_query(conn, query)
if (response == 0) println("Connection successful. Table created") else println("Connection failed. Table not created.") end
execute_query() command, which takes the connection variable and the query as parameters. A sample SELECT query can be executed through the following steps:query = """SELECT * FROM Student;""" data = execute_query(conn, query)
opformat should be specified:data_array = execute_query(conn, query, opformat = MYSQL_ARRAY)
mysql_execute_multi_query() command:query = """INSERT INTO Student (Name) VALUES (''); UPDATE Student SET JoinDate = '08-07-15' WHERE LENGTH(Name) > 5;""" rows = mysql_execute_multi_query(conn, query) println("Rows updated by the query: $rows")
Data handling within a PostgreSQL database can be done by connecting to the database. Firstly, make sure that the database server is up and running. Now, the data in the database can be handled through the following procedure:
import statements:using DBI using PostgreSQL
PostgreSQL library are as follows:DataFrames.jl: This has already been installed previously.DataArrays.jl: This can be installed by running the statement Pkg.add("DataArrays")).conn = connect(Postgres, "localhost", "password", "testdb", 5432)
PostgreSQL.PostgresDatabaseHandle(Ptr{Void} @0x00007 fa8a559f160,0x00000000,false)
query = prepare(conn, "SELECT 1::int, 2.0::double precision,
'name'::character varying, " * "'name'::character(20);"))
result = execute(query)
finish(query) disconnect(conn)
SQLite package into the current session and ensure that the SQLite server is up and running. The package can be imported by running the following command:using SQLite
SQLiteDB() function in Julia Version 3 and the SQLite.DB() function in Julia Version 4.db = SQLite.DB("dbname.sqlite")
db = SQLiteDB("dbname.sqlite")
query() function in Version 3 and the SQLite.query() function in Version 4.query(db, "A SQL query")
SQLite.query(db, "A SQL query")
The SQLite.jl package also allows the user to use macros and registers for manipulating and using data. However, the concepts are beyond the scope of this chapter.
So, these are some of the ways through which data can be handled in Julia. There are a lot of databases whose connectors directly connect to DBI, such as SQlite, MySQL, and so on, and through which queries and their execution can be carried out, as shown in the PostgreSQL section. Similarly, data can be scraped from the Internet and used for analytics, which can be achieved through a combination of Julia libraries, but that is beyond the scope of this book.
The following resource helps you learn more about its advanced features and provides information about the MySQL.jl library of Julia. This includes performance benchmarks and details, as well as information on CRUD and testing:
Visit https://github.com/JuliaDB/DBI.jl to understand better the DBI we use to connect local PostgreSQL databases:
Visit
https://github.com/JuliaDB/DBI.jl
for extended and in-depth documentation on the PostgreSQL.jl library, which includes dealing with Amazon web services, and so on.
Now, as you have learned the ways in which data can be extracted, manipulated, and worked on from various external sources, there are some more interesting things that the database drivers of Julia can do apart from just executing queries. You can find those at https://github.com/JuliaDB/SQLite.jl/blob/master/OLD_README.md#custom-scalar-functions .
Change the font size
Change margin width
Change background colour