MATLAB: Do I receive an “Reference to non-existent field” error when I use FETCH in Database Toolbox 3.4 (R2007b)

databaseDatabase Toolboxfield

I want to fetch some data from my database in a structure so I use the following script:
conn = database('myDB','myUser','myPass','com.mysql.jdbc.Driver','jdbc:mysql://myServer/myDB')
setdbprefs('DataReturnFormat','structure')
data = fetch(conn,'SELECT `type`,`fetch`,`name` FROM testtable')
I expect to get the requested data but instead I receive:
??? Reference to non-existent field 'Type'.
Error in ==> cursor.subsref at 65
eval(tmpcomm)
Error in ==> database.fetch at 68
x.(flds{i}) = [x.(flds{i});e.Data.(flds{i})];
If I use the following query:
data = fetch(conn,'SELECT `fetch`,`name` FROM testtable')
I receive:
??? Reference to non-existent field 'Fetch'.
Error in ==> cursor.subsref at 65
eval(tmpcomm)
Error in ==> database.fetch at 68
x.(flds{i}) = [x.(flds{i});e.Data.(flds{i})];
If I use:
data = fetch(conn,'SELECT `name` FROM testtable')
The data from the name column is returned succesfully; however I also need the data contained in the type and fetch columns. Further, when I use:
c = exec(conn, 'SELECT `type`,`fetch`,`name` FROM testtable');
d = fetch(c)
I do not receive any errors, but soon as I try to access my data using:
d.Data.type
I receive:
??? Reference to non-existent field 'Type'.
Error in ==> cursor.subsref at 65
eval(tmpcomm)

Best Answer

If the database table contains columns called any of the following, but with a different case, and the SQL query tries to pull data from this column, this error can be thrown by the Database Toolbox.
Attributes, Data, DatabaseObject, RowLimit, SQLQuery, Message, Type, ResultSet, Cursor, Statement, Fetch
This is because of the fact that the cursor object in MATLAB already contains these members, which will overshadow the ones retrieved from cursor.Data.
To work around this issue, you can first copy the data to another variable and then access the field:
cur = exec(conn, 'SELECT `type`,`fetch`,`name` FROM testtable');
dat = fetch(cur);
myData = dat.Data;
myData.type % This does work
Other option are to rename the column in your database, or to have the data returned in a cell array instead of in a struct, using the following command:
setdbprefs('DataReturnFormat', 'cellarray')