SQL connection: String that read as several cell array
Show older comments
I want to get hourly data from 15-minute data with sqlquery,
connection = database ('databasename', 'username', 'password', 'com.mysql.jdbc.Driver', 'jdbc:mysql:address');
string=['SELECT Hour(inserted_at) AS hour, SUM(duration_consumption) AS SUM_DUR FROM ami_histories WHERE inserted_at >= '' ',date1,' '' AND inserted_at < '' ',date2,' '' AND `cluster`= ',num2str(ami),' GROUP BY hour'];
inserted_at >= ' 2017-10-1 00:00:00 ' AND inserted_at < ' 2017-10-02 00:00:00 ' AND `cluster`= 1 GROUP BY hour']
curs = exec(connection,sqlquery);
row = fetch (curs);
raw_data = row.Data;
dur_consumption = cell2mat(raw_data);
the sqlquery's string should return as sqlquery =
'SELECT Hour(inserted_at) AS hour, SUM(duration_consumption) AS SUM_DUR FROM ami_histories WHERE inserted_at >= ' 2017-10-1 00:00:00 ' AND inserted_at < ' 2017-10-02 00:00:00 ' AND `cluster`= 1 GROUP BY hour'
I have tried that code directly in mysql database, and it is working.
But then, it is read as 1x7 cell array string =
1×7 cell array
'SELECT Hour(inserted_at)…' '2017-10-1 00:00:00' ' ' AND inserted_at < ' ' '2017-10-02 00:00:00' ' ' AND `cluster`= ' '1' ' GROUP BY hour'
To make it as single string, So I put sqlquery=join(string)
Even after I already put single string as sqlquery, the connection doesnt result data that I want, It returns to an error
row =
cursor with properties:
Attributes: []
Data: 0
DatabaseObject: []
RowLimit: 0
SQLQuery: []
Message: 'Invalid Cursor: Second argument must be either a SQL query or object of type sqlOutput'
Type: 'Database Cursor Object'
ResultSet: 0
Cursor: 0
Statement: 0
Fetch: 0
raw_data =
0
Cell contents reference from a non-cell array object.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
Error in hourlyAMI (line 29)
dur_consumption = cell2mat(raw_data)
What did I do wrong?
Answers (0)
Categories
Find more on Database Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!