Querying through a vector
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I have configured matlab to read a database located in postgresql. At the moment is working fine if the query implies only one element. However, in the application where this is going to be used it will need to read a vector of data (i.e Lat = [90 80 70 60 50 ....]) and when I have been trying to do that with my current code is not querying the values.
Here is my code to see if you could detect something wrong in it.
I would appreciate any hint or help to try to solve this issue.
Regards
tic
conn = database('mydb', 'postgres', 'postgres', 'Vendor', 'PostgreSQL', 'Server', 'localhost');
sqlquery = ['select Att from coordinate6 where Lat = ' num2str(lat) 'AND Lon = ' num2str(lon) 'AND Height = ' num2str(height) 'AND Elev = ' num2str(elev) 'AND Freq = ' num2str(freq) 'AND Ant_Diam = ' num2str(ant_diam) 'AND Avail = ' num2str(avail)];
curs = exec(conn,sqlquery);
curs = fetch(curs);
curs.Data
toc
2 个评论
the cyclist
2017-1-6
What do you get for a result, and what did you expect to get? If you don't get a result, what is the complete error message you are getting?
回答(1 个)
Guillaume
2017-1-9
编辑:Guillaume
2017-1-9
With your latest comment, have you actually looked the content of sqlquery you generate? You'll see it didn't add the latitude (well, it did, each as character ascii 0)
sqlquery = ['SELECT att FROM test2 WHERE lat IN (' num2str(lat) ')'];
Note that I don't know anything about the database toolbox, but if it returns a cursor as it claims, then you probably need to move the cursor to access each row returned by the query.
2 个评论
Guillaume
2017-1-11
"it's not working" is not helpful. We need to know the details of the error if one occur, or in what way it's not working otherwise.
I should have reminded myself of the syntax of the IN clause. You need a comma between each value, so:
sqlquery = sprintf('SELECT att FROM test2 WHERE lat IN (%s)', ...
strjoin(arrayfun(@num2str, lat, 'UniformOutput', false), ','))
or, using the new string class in R2016b:
sqlquery = compose('SELECT att FROM test2 WHERE lat IN (%s)', ...
join(string(lat), ','))
另请参阅
类别
在 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!