How to update database with whereclause using variable as ID and launching it from a GUI

조회 수: 1 (최근 30일)
[EDIT: 20110725 09:37 CDT - reformat - WDR]
I prepared a table called 'Table1' with MS Access as shown below
ID Harzards Location Magnitude Casualties Aids DeathRate
1 123 345 321 567 789 456
2 125 675 234 567 123 456
3 123 45 56 23 45 569
4 65 78 34 12 45 579
8 1 123 345 321 567 789
9 2 125 675 234 567 123
10 3 123 45 56 23 345
I used the following code under the pushbutton callback function to launch the update and I had the following error
code:
colnames = {'Harzards','Location','Magnitude','Casualties',Áids','DeathRate'};
conn = database('grace',' ',' ');
set(conn, 'ÁutoCommit', 'on');
get(conn, 'ÁutoCommit');
update(conn, 'Table1', colnames,{handles.edit1,handles.edit2,handles.edit3,handles.edit4,handles.edit5,handles.edit16}, {'where ID = handles.edit28'});
The values to all the handles are specified on the GUI interface
Error generated:
Error in ==> retrieve2withD>pushbutton3_callback at 383
update(conn, 'Table1', colnames,{handles.edit1,handles.edit2,handles.edit3,handles.edit4,handles.edit5,handles.edit16}, {'where ID = handles.edit28'});
retrieve2withD is the M-file name for the GUI interface.
Please help me out, how do I replace data in the database. Thanks.
  댓글 수: 1
Oleg Komarov
Oleg Komarov 2011년 7월 25일
PLease format the code: http://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question

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

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 7월 25일
You cannot supply the where clause in that way.
You are saying update the columns indicated in colnames with the values contained in the cell array of handle.edit# where the ID = 'handles.edit28', but you want to retrieve the value of handles.edit28.
So, if handles.edit28 is a single values, then:
update(...,['where ID = ' sprintf('%f',handles.edit28)])
or if multiple values:
update(...,['where ID in(' sprintf('%f',handles.edit28(1)) sprintf(',%f',handles.edit28(1)) ')'])
The third method is listed in the documentation and it poses a specific where clause column by column.
  댓글 수: 2
MEP
MEP 2023년 8월 10일
This work also for me but when I want compare an array of values for example if I have not handles.edit28 but an array of elements?

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by