How to Build a Database query

조회 수: 4 (최근 30일)
shaz
shaz 2012년 4월 2일
Hi i have 3 variables one say Columnname tells about the database column names ,other two variable2 & variable3 tells about timestamp for eg( 03-Apr-2012 12:25:00 ).
Columnname = abcd variable2 = 03-Apr-2012 12:25:00 variable3 = 10-Apr-2012 12:25:00
after connecting to database & building query like this
data=exec(connection,'SELECT Columnname FROM TABLENMAE WHERE Columnname >= (''variable2 '') AND Columnname < (''variable3 '') ');
but i am getting data as blank.feels some issues while building query
Thanks in advance

채택된 답변

Titus Edelhofer
Titus Edelhofer 2012년 4월 2일
Hi,
it should probably read
data=exec(connection, ['SELECT ' Columnname ' FROM TABLENMAE WHERE ' Columnname '>=("' variable2 '") AND ' Columnname ' < ("' variable3 '") ']);
Two suggestions: it's easier if you do something like
sqlstatement = ['SELECT ...' ];
data = exec(connection, sqlstatement);
because then you can put a breakpoint on the exec line and see if your sqlstatement looks fine (e.g. copy paste to database client).
Second: use querybuilder to build your sql query ...
Titus
  댓글 수: 2
shaz
shaz 2012년 4월 2일
Thanks for replying..but have some issues while executing
??? No constructor 'com.mathworks.toolbox.database.sqlExec' with matching signature
found.
Error in ==> cursor.cursor at 60
curs.Cursor = com.mathworks.toolbox.database.sqlExec(curs.SQLQuery ,connect.Handle);
Error in ==> database.exec at 34
curs=cursor(connect,sqlQuery);
Titus Edelhofer
Titus Edelhofer 2012년 4월 2일
Hi,
did you try the second? How does sqlstatement look like? Is it perhaps a cell array (because one of your variables is a cell array?). Then you would need to replace (e.g.) Columnname by Columnname{1} ...
Titus

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by