It seems that Julia database tutorials that are on the web are outdated. Are there any simple examples of Julia and, say, MySQL.jl usage?
Hi Miko, if you are looking for first stepsβ¦
julia> using MySQL, DataFrames
julia> dbconn = DBInterface.connect(MySQL.Connection, "127.0.0.1", "wiag", "secret pwd", db = "wiag2")
This opens a database connection on host 127.0.0.1 (localhost) for user βwiagβ identified with βsecret pwdβ on database βwiag2β. Then you can use the connection for queries or any other SQL-statement:
julia> sql = "SELECT id, givenname, familyname FROM person LIMIT 12"
julia> df_p = DBInterface.execute(dbconn, sql) |> DataFrame;
julia> df_p
12Γ3 DataFrame
Row β id givenname familyname
β Int32 String String?
ββββββΌββββββββββββββββββββββββββββββ
1 β 80 Ortlieb Brandis
2 β 81 Jean Bourgeois
3 β 82 Louis Bourbon
4 β 83 Radbod missing
5 β 84 Egilbert missing
6 β 85 Adelbold missing
7 β 86 Alfrid missing
8 β 87 Hunger missing
9 β 88 Liudger missing
10 β 89 Eginhard missing
11 β 90 Alberich missing
12 β 91 Friedrich missing
The result of the query is used here to create a DataFrame
.
As an alternative you may want to iterate over the result set:
julia> qi = DBInterface.execute(dbconn, sql);
julia> for row in qi
println(row[:id], " ", row[:givenname])
end
80 Ortlieb
81 Jean
82 Louis
83 Radbod
84 Egilbert
85 Adelbold
86 Alfrid
87 Hunger
88 Liudger
89 Eginhard
90 Alberich
91 Friedrich
3 Likes
Thank you!