Unmatched parameter name error when connecting to a PostgreSQL database

26 ビュー (過去 30 日間)
Daniele Lupo
Daniele Lupo 2019 年 11 月 1 日
コメント済み: Andrew Stamps 2020 年 6 月 16 日
I want to connect to a PostgreSQL database. I've the database toolbox.
When I insert data i obtain an error:
databaseLink = "jdbc:postgresql://" + options.getHostname() + ":" + options.getPort() + "/" + options.getDatabaseName();
this.connection = database( ...
options.getDatabaseName(), ...
options.getUsername() , ...
options.getPassword() , ...
"org.postgresql.Driver" , ...
databaseLink ...
);
The error is
Error using database (line 59)
Unmatched parameter name 'org.postgresql.Driver' must be a string scalar or character vector that can represent a field name.
How can I fix this error?
EDIT:
I went on the error line and I've found that It's connecting to database inside this:
function connection = database(varargin)
%DATABASE Connect to database.
% CONNECT = DATABASE(DATASOURCE,USERNAME,PASSWORD, 'PARAM', 'VALUE')
% returns a database connection object. Datasource can be a JDBC or ODBC
% data source. Username and Password should be provided as required for
% authorization and authentication by the database.
%
% Parameter Value Default
% ------------ ------------ -------
% AUTOCOMMIT 'on' | 'off' 'on'
% LOGINTIMEOUT 0 | double 0
% READONLY 'on' | 'off' 'off'
% ERRORHANDLING 'report' | 'store' setdbprefs('ErrorHandling')
%
% Example:
% --------
%
% JDBC connection:
% ----------------
% conn=database('jdbcdatasource','scott','tiger')
%
% where:
%
% 'jdbcdatasource' is the JDBC datasource name created using configureJDBCDataSource.
% 'scott' is the user name.
% 'tiger' is the password.
%
%
% ODBC connection:
% ----------------
% conn=database('odbcdatasource','scott','tiger')
%
% where:
%
% 'odbcdatasource' is the ODBC datasource name created using configureODBCDataSource.
% 'scott' is the user name.
% 'tiger' is the password.
%
% database methods:
% -----------------
% sqlwrite - Insert MATLAB data into database table.
% sqlread - Import data into MATLAB from database table.
% sqlfind - Find information about all table types in
% database.
% sqlinnerjoin - Inner join between two database tables.
% sqlouterjoin - Outer join between two database tables.
% execute - Execute SQL statement using relational
% database connection.
% fetch - Import data into MATLAB using connection handle.
% isopen - Detect if database connection is open/close.
% close - Close database connection.
% commit - Make database changes permanent.
% rollback - Undo database changes.
% update - Replace data in database table with data from MATLAB.
%
% Copyright 1984-2019 The MathWorks, Inc.
connection = database.internal.utilities.hdatabase(varargin{:});
end
I've put a breakpoint in there and varargin is:
varargin =
1×5 cell array
{["wtrade"]} {["postgres"]} {["squall"]} {'org.postgresql.Dr…'} {["jdbc:postgresql…"]}
But from the description of the function instead of 'org.postgresql.Driver' there should be AUTOCOMMIT, LOGINTIMEOUT, READONLY or ERRORHANDLING. So this is the error. But I'm using the database function in the right way, according to documentation example, so why I obtain this error, and how can I connect in the right way?

採用された回答

Daniele Lupo
Daniele Lupo 2019 年 11 月 4 日
I've got the solution by myself, even if it's not as straightforward as I've supposed.
My database name is wtrade.
In order to test it, I opened the database explorer of matlab and I've created a new connection. I called this connection wtrade as well.
When I call the database function in order to get the connection, the inner code at first look if there's a connection with the name that I give with first argument, and if not it goes ahead. Since I had a connection named the same way as the database, it started the initialization code of following signature:
conn = database(datasource,username,password)
instead of this one
conn = database(databasename,username,password,driver,url)
since datasource and databasename were the same.
The first signature accept name values pairs Name, value, and since driver value is not one of accepted parameter values of course, like "Name" or "Vendor" (check database documentation for this), I got the error, that says clearly (now) that the string in "driver" position is not the name of an accepted parameter like "Name" or "Vendor"
In order to solve it I've deleted the connection that I've created with database explorer and it works. My opinion anyway is that's a bug, since it's normal and not forbidden to have a datasource with the same name of the database.
  2 件のコメント
the cyclist
the cyclist 2019 年 11 月 4 日
Glad you figured it out!
Andrew Stamps
Andrew Stamps 2020 年 6 月 16 日
This is subtle, and it got me too. Glad I found this post, as it saved me a bunch of time hunting down the issue.

サインインしてコメントする。

その他の回答 (2 件)

the cyclist
the cyclist 2019 年 11 月 1 日
編集済み: the cyclist 2019 年 11 月 1 日
I am not able to test this right now, but it seems possible to me that MATLAB wants single quotes instead of double quotes around org.postgresql.Driver.
The error message says a string scalar should be ok, so I'm pessimistic that this is the solution. But I've only ever used single quotes there, and it is an easy thing to try.
Similarly, the complaint might actually be about the value in that name-value pair. So maybe make databaseLink into a character array instead of a string as well.
  1 件のコメント
Daniele Lupo
Daniele Lupo 2019 年 11 月 1 日
I've already tried single quotes, but it's not the problem.

サインインしてコメントする。


the cyclist
the cyclist 2019 年 11 月 3 日
Sorry for the delayed reply, and I truly don't know if this will help or not. It does not seem to directly related to your error message, but I figured it doesn't hurt to suggest it!
In my equivalent of your databaseLink variable, my string ends with the port number and then a slash. It does not have the database name as part of it.
  1 件のコメント
Daniele Lupo
Daniele Lupo 2019 年 11 月 4 日
Mine has the database name as per doc example. Once solved the problem (see below) it worked.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by