When writing in an already existing table - JDBC driver error relation already exists

4 views (last 30 days)
Hi,
I'm trying to insert information obtained and modified from a .csv file to a database through matlab in order to run some tests on the public data.
To this objective I created a Postgresql database and the required table, since from this I got the idea I could pass the table from the matlab workspace onto the database table directly. I setup de JDBC connection, tested and all is okay.
However when I run the sqlwrite command, though it can create new table with the information I give, if table already exists but is empty, it simply gives the error:
Error using database.jdbc.connection/sqlwrite (line 104)
JDBC JDBC/ODBC Error: JDBC Driver Error: ERROR: relation
"ozonezurich.testschema.ozonetable" already exists.
The code,
filename = 'test.csv';
Table = Read_csv(filename);
datasource = "zurichtest";
username = "postgres";
password = "************";
conn = database(datasource,username,password);
ColumnNames = {'ObsDate','TimeStamp','Latitude','Longitude','HDOP', ...
'DeviceID','O3ppB','Temperature','Humidity'};
Table.Properties.VariableNames = ColumnNames;
sqlwrite(conn,'"ozonezurich.testschema.ozonetable"',Table(1:100,:))
close(conn);
Does anyone know how can I fix this?
P.S: Having the .csv file I know I can run the tests, however, further ahead on the project I'll need to do this or something similar. Since I'll be processing data and will be needing to write it on a database.
  1 Comment
Luís Marques
Luís Marques on 23 Aug 2019
Edited: Luís Marques on 23 Aug 2019
Issue solved.
Theres was some issue with prior instructions I tried that ended up creating a bunch of tables (different names) on the database but on the default schema created by postgres.
The thing is that the table I was trying to append to already existed on that default schema, and due to most likely not specifying the schema correctly an error was ocurring.
Later, another issue was raised (which was rasied before and most likely prompted the cascade of wrong instructions I gave the database) concerning the compatibility of the datatypes, with me trying to write datatypes not supported.

Sign in to comment.

Accepted Answer

Urmila Rajpurohith
Urmila Rajpurohith on 23 Aug 2019
Try the example mentioned in the Documentation provided in the below link
If it does not work as expected, try to reinstall the JDBC Driver and try again.
Follow the below mentioned documentation link for establishing connection to the data base
  1 Comment
Luís Marques
Luís Marques on 23 Aug 2019
Hi, thank you for your answer.
I eventually solved the problem though I forgot to close the subject by commenting my original post. Will do that now.
Cheers

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!