Skip to content
Advertisement

Julia: How to get dataframe value into single variable

I am modifying Julia code that I did not write myself. I have worked very little in Julia. I’ve concatenated a string to create a SQL Query that returns a single integer value from the database. I want to use the returned value in the next sql string. However, the value returned from the initial SQL Select statement is a 1×1 dataframe. My assumption is I need to assign returned value to a integer variable that can be used in the subsequent SQL String.

import Dates
using ODBC
using Dataframes

dt = Dates.today()
db = ODBC.DSN("mydsn","myuser","mypasswd")
sqlstr = string("SELECT empid FROM employees WHERE name = ","'","John Doe","'"," AND startdate < ","'",dt,"'",")
relempid = ODBC.query(db, sqlstr)

Based on the returned empid I want to run additional SQL Query that will return a dataframe from the database.

sqlstr2 = string("SELECT ticketId, shortdescription FROM tickets WHERE empid = ","'",relempid,"'",")

df = ODBC.query(db, sqlstr2)
ODBC.ODBCError("API.SQLExecDirect(stmt, query) failed; return code: -1 => SQL_ERROR

However, this results in the above SQL error as relempid is itself a 1×1 dataframe.

I’m sure this is probably simple enough, but I can’t manage to get it working.

Advertisement

Answer

I understand you have a DataFrame like this:

julia> using DataFrames

julia> df = DataFrame(a=111)
1×1 DataFrame
 Row │ a
     │ Int64
─────┼───────
   1 │   111

In that case simply note that DataFramess can be indexed just like matrices:

julia> df[1,1]
111

Of course you could also reference the value by column name:

julia> df.a[1]
111

julia> df[1, :a]
111
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement