How to update database with whereclause using variable as ID and launching it from a GUI
1 view (last 30 days)
Show older comments
[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 Comment
Oleg Komarov
on 25 Jul 2011
PLease format the code: http://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question
Accepted Answer
Oleg Komarov
on 25 Jul 2011
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 Comments
MEP
on 10 Aug 2023
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?
More Answers (0)
See Also
Categories
Find more on Database Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!