function with single-string argument, querying sql database

1 view (last 30 days)
Hi everyone
Please see below my codes aiming at creating a function which queries data from my database:
function x=getsqldata(var);
Set preferences with setdbprefs.
setdbprefs('DataReturnFormat', 'cellarray');
setdbprefs('NullNumberRead', 'NaN');
setdbprefs('NullStringRead', 'null');
% Set maximum time allowed for establishing a connection.
timeout=logintimeout(10);
%Make connection to database. Note that the password has been omitted.
%Using ODBC driver.
conn = database('odbccalc', 'xx', 'xx');
% Check the database status.
ping(conn);
%Read data from database.
var;
curs = exec(conn, ['SELECT d.ISN , d.Date, d.Px_last FROM tblData d WHERE d.ISN=''',var,'''']);
curs = fetch(curs);
close(curs);
%Assign data to output variable
x = curs.Data;
%Close database connection.
close(conn);
%Clear variables
clear curs conn
While running the function from the command window, I end up with:
getsqldata('CAIXUSD');
Undefined function 'Set' for input arguments of type 'char'.
Thus I wonder if someone could assist. _ In a nutshell, how to make a string variable a valid argument for this function_?
Thanks in advance
ps: the query statements works, I have tested already.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!