this example in the ODBC documentation for how to execute a prepared statement doesn’t seem to work
# prepare a statement with 3 parameters marked by the '?' character
stmt = ODBC.prepare(dsn, "INSERT INTO cool_table VALUES(?, ?, ?)")
# a DataFrame with data we'd like to insert into a table
df = DataFrame(col1=[1,2,3], col2=[4.0, 5.0, 6.0], col3=["hey", "there", "sailor"])
for row = 1:size(df, 1)
# each time we execute the `stmt`, we pass another row to be bound to the parameters
ODBC.execute!(stmt, [df[row, x] for x = 1:size(df, 2)])
end
ERROR: ODBC.ODBCError("API.SQLExecute(stmt) failed; return code: -1 => SQL_ERROR")
Can someone check the syntax in the documentation?
I should note, that inserting the values directly works OK, so it’s not the table definition or the values. It looks like the prepared statement doesn’t work as documented.
Sorry, I’m not clear on your response. Are you saying that the documented example in the ODBC online documentation is correct and working for you, or are you saying that it is wrong, and it will be changed?
I’m specifically asking about the prepared statement example shown in the ODBC documentation here :Home · ODBC.jl
Under the section that starts “ODBC.prepare”
# prepare a statement with 3 parameters marked by the '?' character
stmt = ODBC.prepare(dsn, "INSERT INTO cool_table VALUES(?, ?, ?)")
# a DataFrame with data we'd like to insert into a table
df = DataFrame(col1=[1,2,3], col2=[4.0, 5.0, 6.0], col3=["hey", "there", "sailor"])
for row = 1:size(df, 1)
# each time we execute the `stmt`, we pass another row to be bound to the parameters
ODBC.execute!(stmt, [df[row, x] for x = 1:size(df, 2)])
end
…
Consult your DB/vendor-specific SQL syntax for the exact specifications for parameters.
Examples:
… code …
so SQL syntax for the prepared statement is varying by the context of the DB that you use.
I commented about the another package to solve this problem a bit.
This SQL syntax is correct as I mentioned previously, if I run it manually, it is successful.
"INSERT INTO cool_table VALUES(?, ?, ?)"
It’s the last execution of the prepared statement that isn’t working. The following part shouldn’t be from the DB vendor. This is pure Julia syntax.
for row = 1:size(df, 1)
# each time we execute the `stmt`, we pass another row to be bound to the parameters
ODBC.execute!(stmt, [df[row, x] for x = 1:size(df, 2)])
end
I get the same issue. Could it be because I’m on 1.0.2 Julia and ODBC is 0.8.1
I’m also struggling to find a way to use this to connect to servers without being forced into using a system DSN (windows 10)
For example we are using FQD name to find out SQL boxes such as abc2012-sql.company.com,1234 this way we can move our servers and all our applications keep on working as we update the DNS on our network. I’d ideally need to have this directly in my Julia code. When I tried using the System DSN, julia ignores the supplied user+password because it gets it from the DSN instead which is too limiting (DSN is setup to read only for a reason on our machines across our network)