sql query not working
2 views (last 30 days)
Show older comments
In my code, there is a comparison of database record value stored in a variable and to fetch the corresponding record in the database using matlab. Please help me and the code is given below. here showing curs.Data is 0.
function ImagesExample(uname1)
%# read images in a cell array
disp(uname1);
imgs = cell(6,1);
for i=1:6
imgs{i} = imread( sprintf('C:/Users/maruthi1/Documents/MATLAB/images3/ma3%1d.jpg',i) );
end
%# show them in subplots
figure('Name','Image_mag','Numbertitle','off');
title('Image_mag');
for i=1:6
subplot(2,3,i);
h = imshow(imgs{i}, 'InitialMag',200, 'Border','tight');
title(num2str(i))
set(h, 'ButtonDownFcn',{@callback,i})
end
function callback(o,e,idx)
%# show selected image in a new figure
figure(2), imshow(imgs{idx})
title(num2str(idx))
promptMessage = sprintf('Drag out a box that you want to copy,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
A=imgs{idx};
n = input('Please enter the number of scenes you wish to crop: ')
ii=1;
%load user.mat
conn = database('mani','root','MARUTHI');
%setdbprefs('DataReturnFormat','cellarray');
curs=exec(conn,'select mobile_num from user2 where uname IN(uname1)');
%im_cropped = imcrop(A);
%B=imgs{idx};
%im_cropped = imcrop(B);
curs = fetch(curs);
curs.Data
m=curs.Data;
%C=imgs{idx};
while (ii<=n)
%[im_cropped rect] = imcrop(A);
im_cropped{ii} = imcrop(imgs{idx});
filename = ['images3\images_cropped' num2str(ii) '.jpg'];
imwrite(im_cropped{ii},filename);
ii=ii+1;
end
close(curs);
close(conn);
promptMessage = sprintf('Your cropped images saved successfully');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
end
%croping(A);
end
0 Comments
Answers (1)
Guillaume
on 28 Mar 2017
You've given us a lot of irrelevant code. Your SQL query is:
curs=exec(conn,'select mobile_num from user2 where uname IN(uname1)');
So it's searching for the literal string 'uname1' in the uname column of your user2 table. Is there such a string in that column?
Perhaps, uname1 is supposed to be replaced by the content of the uname1 variable. But matlab cannot guess that a particular arbitrary portion of a string has to be replaced with the content of a variable of the same name. You have to tell it to do so with sprintf or the newer compose:
curs = exec(conn, sprintf('select mobile_num from user2 where uname IN(%s)', uname1));
2 Comments
Guillaume
on 28 Mar 2017
What is displaying as zero?
Is the username found in the uname column of the user2 table. If not, then there's nothing to return.
If the type of the uname column is string. Then the search value need to be enclosed in quotes:
curs = exec(conn, sprintf('select mobile_num from user2 where uname IN (''%s'')', uname1));
Note that I've not changed your SQL. However, the IN clause usually specifies several values to look up, whereas you only have one (I assume), so:
curs = exec(conn, sprintf('select mobile_num from user2 where uname = ''%s''', uname1));
is probably better.
See Also
Categories
Find more on Database Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!