How can i read database from database row by row?
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Geoff
2012년 5월 6일
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.
채택된 답변
Juan B. Gutierrez
2012년 5월 17일
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
댓글 수: 0
추가 답변 (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!