Importing data from Microsoft Access w/ SQL query

2 ビュー (過去 30 日間)
Jared
Jared 2014 年 11 月 29 日
コメント済み: Jared 2014 年 11 月 29 日
I have the database toolbox and have had no issues exporting data from MATLAB into Access using the ODBC connection process. However, it's taken me awhile to figure out how to import data into MATLAB from my Access database using SQL queries in MATLAB. I'm trying to understand why this code doesn't work (I don't have the connection code below but that's not the issue...MATLAB establishes the connection just fine):
A='MKT';
curs = exec(conn,['select Period,Ticker from MonthlyReturns where Ticker = ',A]);
curs = fetch(curs)
While this code does:
A='MKT';
curs = exec(conn, ['select Period,Return from MonthlyReturns where Ticker = ''', A, ''''])
curs = fetch(curs)
Just not clear to me exactly why all the additional apostrophes are needed.
Thanks,
JK

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 11 月 29 日
Jared - look at the two queries (as strings)
>> A = 'MKT';
>> query1 = ['select Period,Ticker from MonthlyReturns where Ticker = ',A];
>> query2 = ['select Period,Return from MonthlyReturns where Ticker = ''', A, ''''];
query1 =
select Period,Ticker from MonthlyReturns where Ticker = MKT
query2 =
select Period,Return from MonthlyReturns where Ticker = 'MKT'
Your Ticker field data type is char/string, so you need to wrap the condition for it (in your where clause) in single quotes. This is just usual SQL syntax (and is not MATLAB related).
  1 件のコメント
Jared
Jared 2014 年 11 月 29 日
Thanks for the help.
JK

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by