Writing a table with dates to sql database
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kevin Blackwell
el 14 de Jul. de 2021
Respondida: MathWorks Computational Finance Team
el 27 de Jul. de 2021
Using the database toolbox I'm haveing issues writing a table to my database that includes dates,
It works fine when the dates are converted to string using the cellstr function but these are not then registered as date values in my database and the column has to be set to text which then leads to issues on the sql side of things.
it there any way of exporting using database toolbox that allows me to keep the format of the date column ?
I've tried converting to a timetable but this isnt accepted by the database toolbox.
0 comentarios
Respuestas (1)
MathWorks Computational Finance Team
el 27 de Jul. de 2021
Hi Kevin,
I am not sure which MATLAB version or database you are working with. But, example I am sharing below assumes you are working with MATLAB R2021a:
% Connect to your database
conn = odbc(...); % For ODBC connection
% OR
conn = database(...); % For JDBC connection
% OR
conn = mysql(...); % For MySQL native connection
% OR
conn = postgresql(...); % For PostgreSQL native connection
% Read data from 'outages.csv'
outages = readtable('outages.csv');
% Use SQLWRITE to insert data into a outages database table.
% In this example the table does not exist in the database. SQLWRITE will create a new database table for you.
% Variables "OutageTime" & "RestoratonTime" in outages MATLAB table are of type datetime.
sqlwrite(conn,'outages',outages)
% Read the data back in MATLAB
opts = databaseImportOptions(conn,'outages');
opts = setoptions(opts,[2 5],'Type','datetime');
T = sqlread(conn,'outages',opts);
Hope this helps answer your question.
Thanks,
Rikin
0 comentarios
Ver también
Categorías
Más información sobre Database Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!