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
2011 年 7 月 25 日
PLease format the code: http://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question
採用された回答
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
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 件)
参考
カテゴリ
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!