How can i read database from database row by row?
6 次查看(过去 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!