Parametrized SQL queries for PostgreSQL?
13 ビュー (過去 30 日間)
古いコメントを表示
Hi, I use Database Toolbox to fetch data from a Postgres database.
Is it possible to parametrize SQL query when using fetch?
conn = postgresql(...);
col4 = "foobar";
query = ...
"SELECT s.col1, s.col2, s.col3 " + ...
"FROM mystuff s " + ...
"WHERE s.col4 = '" + col4 + "'";
fetch_opts = databaseImportOptions(conn, query);
results = fetch(conn, query, fetch_opts);
close(conn);
clear conn query fetch_opts;
Is there a way to declare the col4 as a query parameter istead of concatenating the value into query string?
0 件のコメント
回答 (1 件)
Animesh
2024 年 2 月 15 日
If you are working with a PostgreSQL server and need to execute parameterized queries using MATLAB's database toolbox, you will find that the default “fetch” function does not support this directly. However, you can utilize the “JDBC driver” for PostgreSQL to establish a connection and then create a parameterized query with an “SQLPreparedStatement” object, which is compatible with a JDBC connection. This method allows you to pass the prepared statement to the “fetch” function to retrieve your results.
You can refer the following documentation for more information:
PostgreSQL JDBC for Windows:
SQLPreparedStatement along with example: https://www.mathworks.com/help/releases/R2021b/database/ug/database.preparedstatement.sqlpreparedstatement.html
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!