MatlabのPostgreSQLのリレーショナルデータをクエリするプロジェクトを行っています。私はMatlabとPostgreSQLを接続するためにこの例に従ってきました。PostgreSQLの列名を取得してMatlabの構造体データにインポートする方法
% Add jar file to classpath (ensure it is present in your current dir)
javaclasspath('postgresql-9.0-801.jdbc4.jar');
% Username and password you chose when installing postgres
props=java.util.Properties;
props.setProperty('user', '<your_postgres_username>');
props.setProperty('password', '<your_postgres_password>');
% Create the database connection (port 5432 is the default postgres chooses
% on installation)
driver=org.postgresql.Driver;
url = 'jdbc:postgresql://<yourhost>:<yourport>/<yourdb>';
conn=driver.connect(url, props);
% A test query
sql='select * from <table>'; % Gets all records
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
% Read the results into an array of result structs
count=0;
result=struct;
while rs.next()
count=count+1;
result(count).var1=char(rs.getString(2));
result(count).var2=char(rs.getString(3));
...
end
は私がするResultSetMetaData
rsmd = rs.getMetaData();
columnsNumber = rsmd.getColumnCount();
name1 = rsmd.getColumnName(1);
name2 = rsmd.getColumnName(2);
を使用してPgResultSetから列名を取得することができるが、私は、クエリの結果にそれを設定することができません
while rs.next()
count=count+1;
result(count).var1=char(rs.getString(2));
result(count).var2=char(rs.getString(3));
...
end
ザ・var1とvar2は構造体データの結果の列の名前として表示され、変数name1とname2を代入すると、構造体結果の "var1"と "var2"の代わりに "name1"と "name2"代わりにf変数name1とname2に設定したPostgreSQLのカラム名。
ありがとうございました!
:あなたは別の 構造で、各個別のタプルの結果を持っているしたい場合は、次のコードを使用することができますか?あなたは何を使っていますか? – Adriaan
こんにちは、私はこれに2つのメインプラットフォームMatlabとPostgreSQLを使用しています。このコードはMatlabを接続するために使用され、PostgreSQLは主にJavaで書かれています。 – Trung