function with single-string argument, querying sql database
2 次查看(过去 30 天)
显示 更早的评论
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.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Database Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!