How can i read database from database row by row?

8 views (last 30 days)
i need to select from database row by row this database built on appserve.
i need to read first row then second but i can't I try a lot . when i select all from database .all field are shown but i need to select row by row ,
also I can't get the true size of database by this function
size('my database name')
or
length('my database name')
I write in my code by using size of database
for i=1:c
for j=1:r
output=x(j,i)
%out=x(i,1)
end
end
cell2mat(x)
x(2,2)
%
  2 Comments
Geoff
Geoff on 6 May 2012
Why do you want to read one row at a time? What is wrong with selecting all rows at once? You haven't shown the database query that you are using.
Eman  Shaltout
Eman Shaltout on 7 May 2012
I select all row and put result in variable but I need to search by row.....
QUERY = 'SELECT Feature , Feature2 , Feature3 FROM members '
[x]=fetch(dbConn,QUERY)

Sign in to comment.

Accepted Answer

Juan B. Gutierrez
Juan B. Gutierrez on 17 May 2012
Dear Eman, let me start by saying that you can access any relational database from MATLAB using many tools, including the Database toolbox, and the .NET and/or Java libraries.
The following example uses .NET data objects to connect to a MySQL database (you can connect with this method to ANY database; simply use the correct connection string), send a query, read row by row, and then load dataset columns into MATLAB variables. It is an expandeded version of the solution found at http://stackoverflow.com/questions/6593485/how-to-connect-to-microsoft-sql-server-2008-mssql-from-matlab. I hope this helps.
try
% Database conectivity parameters
host = '192.168.1.3'; % Insert your server's IP here
port = '3306'; % Default port for MySQL
user = 'yourUserName';
password = 'youUserPWD';
dbName = 'yourDatabase';
connString = ['Server=' host ';Port=' port ';Database=' dbName ...
';Uid=' user ';Pwd=' password ...
';Driver={MySQL ODBC 5.1 Driver};Option=3;'];
% Generate SQL query based upon input
sSQL = ['select * from table where' variableWithSQLConditions];
sSQL = char(sSQL);
% Connect to the database
var1 = []; var2 = []; var3=[];
NET.addAssembly('System.Data');
import System.Data.*
conn = Odbc.OdbcConnection(connString);
conn.Open();
dr = Odbc.OdbcCommand(sSQL, conn);
% Retrieve a record at a time
r = dr.ExecuteReader();
while r.Read()
var1 = [var1; str2double(char(r.GetString(0)))];
var2 = [var2 ; str2double(char(r.GetString(1)))];
var3 = [var3 ; str2double(char(r.GetString(2)))];
end
% Close connection
r.Close()
conn.Close()
catch err
cVars = err.message;
end

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!